如何根据组合框中的选定值在DataGridView中显示数据?

发布于 2025-02-03 21:00:42 字数 935 浏览 3 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2025-02-10 21:00:42

对您的代码进行了更改,请尝试以下代码

    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";

        //Change query like this
        string query = "Select * from tblDeliveryLog WHERE customerid = @customerid";

        //Add parameter to query
        cmd.Parameters.AddWithValue("@customerid", TextBoxCustomerId.Text);

        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);
    }

Made changes to your code, try below code

    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";

        //Change query like this
        string query = "Select * from tblDeliveryLog WHERE customerid = @customerid";

        //Add parameter to query
        cmd.Parameters.AddWithValue("@customerid", TextBoxCustomerId.Text);

        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);
    }
司马昭之心 2025-02-10 21:00:42

目前尚不清楚网格中的字段以及组合框如何填充数据。假设组合框被填充了一个“ customerID”为valuemember作为您的先前问题显示,并且在dataTable DT中有一个“ cultueritID”字段,则用作用作网格的数据源对应于组合框valuemember,然后简单地“过滤”网格中的现有数据可能会更容易,而不是重新征服数据库。

使用DataTable,我们可以使用Dataveiew来过滤网格数据。但是,我们需要将网格dataSource“切换”到每个过滤dataview。换句话说,我们无法“直接”过滤DataTable,并自动更新网格。

幸运的是,bindingsource将允许我们过滤它,并且它将自动更新网格,我们不必“切换”网格dataSource

因此,在当前代码中,您可以添加/编辑以下代码将网格数据源设置为bindingsource。类似……

BindingSource OrdersBS;   // <- make a GLOBAL variable 


OrdersBS = new BindingSource();
OrdersBS.DataSource = dt;
dataGridView1.DataSource = OrdersBS;

然后在组合框中selectionIndexchanged事件我们可以根据组合框中选择的值轻松过滤网格。注意:我在第一个位置的组合框中添加了一个附加项目,其值为零(0)和客户的名称为“ all”。这将允许用户“未过滤”网格数据。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
  if ((int)comboBox1.SelectedValue != 0) {
    OrdersBS.Filter = "CustomerID = '" + (int)comboBox1.SelectedValue + "'";
  }
  else {
    OrdersBS.Filter = "";
  }
}

我希望这有意义并有所帮助。

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 the DataTable dt that is used as a data source to the grid that corresponds to the combo boxes ValueMember, 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 a DataView to filter the grids data. However, we would need to “switch” the grids DataSource to each filtered DataView. In other words, we can not “directly” filter the DataTable 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 grids DataSource.

Therefore, in the current code, you could add/edit the following code to set the grids data source to a BindingSource. Something like…

BindingSource OrdersBS;   // <- make a GLOBAL variable 


OrdersBS = new BindingSource();
OrdersBS.DataSource = dt;
dataGridView1.DataSource = OrdersBS;

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.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
  if ((int)comboBox1.SelectedValue != 0) {
    OrdersBS.Filter = "CustomerID = '" + (int)comboBox1.SelectedValue + "'";
  }
  else {
    OrdersBS.Filter = "";
  }
}

I hope this makes sense and helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文