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