BindingSource.Filter
将过滤器应用于已加载到您的DataGridView
中的数据。所以如果你有帐户显示,并且你正在寻找一个特定的帐户,那么你会按帐号过滤这些数据。
Filter
,无法为您运行另一个SQL查询。
如果您要对该数据执行INNER JOIN
,则需要执行另一次搜索并加载DataGridView
。这听起来像你不想执行过滤器,但你想要在同一个网格上显示两个独立的数据集。
你的基本过程是:你的DataGridView
- 负载
- 使用选择过滤器或者他们希望
- 执行与你内心的第二搜索论坛
- 刷新你的DataGridView新纪录数据。这里
编辑是一些代码,可能让你开始:
How to: Bind Data to the Windows Forms DataGridView Control
private void GetData(string selectCommand)
{
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (SqlException)
{
MessageBox.Show("To run this example, replace the value of the " +
"connectionString variable with a connection string that is " +
"valid for your system.");
}
}
private void Button1_Click(object sender, EventArgs e)
{
// Bind the DataGridView to the BindingSource
// and load the data from the database.
dataGridView1.DataSource = bindingSource1;
GetData("select * from Customers");
}
一旦数据被绑定到网格,那么你会为用户提供一些方法来过滤,但你希望他们再次搜索数据。因此,您将希望在代码中使用某种方式来选择要运行的SQL。所以,你可以通过SQL到的GetData()如你已经根据用户的选择
private void Button1_Click(object sender, EventArgs e)
{
// Bind the DataGridView to the BindingSource
// and load the data from the database.
dataGridView1.DataSource = bindingSource1;
string sqlQuery = string.Empty;
if(your check goes here)
{
sqlQuery = "select * from Customers";
}
else
{
sqlQuery = "your new SQL";
}
GetData(sqlQuery);
}
,那么你会根据新的查询重新绑定您的数据集的字符串。
是的,但我很困惑。在我使用VS10中的可视化数据绑定器创建数据集,绑定源并使用可视化工具来配置我的列之前,它是标头。所以用你的方法,我如何重新加载我的新数据并执行Inner Join查询? – 2012-04-12 17:14:12
你有代码绑定数据到你的数据网格吗?如果没有,那么我会为你找到一个例子。 – Taryn 2012-04-12 17:19:59
到目前为止,我有0行代码。一切都在视觉上完成。但是两种方式可以同时存在吗? – 2012-04-12 17:34:55