如何根据组合框中的选定值在DataGridView中显示数据?
我有一个ComboBox,可以从MS Access DB中列出客户名称。当我选择一个客户名称时,它将使用相应的CustomerId填充文本框。我有一个DataGridView从表中显示数据,但是,如何指定仅显示与特定客户ID的数据?
我的dataGridView的代码如下;
private void btnLoadDeliveryLog_Click(object sender, EventArgs e)
{
try
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Carmine_Cycle_Couriers_Database.accdb");
cn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
string query = "Select * from tblDeliveryLog";
cmd.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
}
I have a ComboBox that lists Customer Names from an MS Access DB. When I select a customer name it populates a textbox with the corresponding CustomerID. I have a DataGridView which displays data from a table, however, how do I specify that only the data relating to the specific customer ID is shown?
The code for my DataGridView is as follows;
private void btnLoadDeliveryLog_Click(object sender, EventArgs e)
{
try
{
OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Carmine_Cycle_Couriers_Database.accdb");
cn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = cn;
string query = "Select * from tblDeliveryLog";
cmd.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
cn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚网格中的字段以及组合框如何填充数据。假设组合框被填充了一个“ customerID”为
valuemember
作为您的先前问题显示,并且在dataTable DT
中有一个“ cultueritID”字段,则用作用作网格的数据源对应于组合框valuemember
,然后简单地“过滤”网格中的现有数据可能会更容易,而不是重新征服数据库。使用
DataTable
,我们可以使用Dataveiew
来过滤网格数据。但是,我们需要将网格dataSource
“切换”到每个过滤dataview
。换句话说,我们无法“直接”过滤DataTable
,并自动更新网格。幸运的是,
bindingsource
将允许我们过滤它,并且它将自动更新网格,我们不必“切换”网格dataSource
。因此,在当前代码中,您可以添加/编辑以下代码将网格数据源设置为
bindingsource
。类似……然后在组合框中
selectionIndexchanged
事件我们可以根据组合框中选择的值轻松过滤网格。注意:我在第一个位置的组合框中添加了一个附加项目,其值为零(0)和客户的名称为“ all”。这将允许用户“未过滤”网格数据。我希望这有意义并有所帮助。
It is not clear what fields are in the grid and how the combo box is filled with data. Assuming the combo box is filled with a “CustomerID” as a
ValueMember
as your previous question shows and that there is a “CustomerID” field in theDataTable dt
that is used as a data source to the grid that corresponds to the combo boxesValueMember
, then it may be easier to simply “filter” the existing data in the grid as opposed to re-querying the DB.With a
DataTable
we could use aDataView
to filter the grids data. However, we would need to “switch” the gridsDataSource
to each filteredDataView
. In other words, we can not “directly” filter theDataTable
and have it automatically update the grid.Fortunately, a
BindingSource
WILL allow us to filter it and it will automatically update the grid and we never have to “switch” the gridsDataSource
.Therefore, in the current code, you could add/edit the following code to set the grids data source to a
BindingSource
. Something like…Then in the combo boxes
SelectedIndexChanged
event we could easily filter the grid based on what value is selected in the combo box. NOTE: I added an addition item to the combo box in the first position with the value of zero (0) and the customer’s name of “All.” This will allow the user to “un-filter” the grid data.I hope this makes sense and helps.