如何使用C#中的多个组合框过滤datagridview? [英] How to filter datagridview using multiple combo boxes in C#?

查看:69
本文介绍了如何使用C#中的多个组合框过滤datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


大家好!

解决方案

嗨rockovo,


不应该太难了将其他组合框添加到表单后,只需添加其他代码,类似于您已有的名称。并更改PopulateDataGridView以添加其他参数。我将向你展示一个示例
,为角色添加一个ComboBox:


 private void frmEmpList_Load(object sender,EventArgs e)
{
this.PopulateComboBoxes();
this.PopulateDataGridView();
}
private void PopulateComboBoxes()
{
this.PopulateComboBoxLastName();
this.PopulateComboBoxRoles();
}
private void PopulateComboBoxLastName()
{
//与你已有的相同代码...
}
private void PopulateComboBoxRoles()
string query =" SELECT DISTINCT Role FROM Employees" ;; //或者任何正确的SELECT将是
string constr = @" Data Source = DESKTOP-Q2B3UUH\SQLEXPRESS; Initial Catalog = PeopleManager; Integrated Security = True" ;;
using(SqlConnection con = new SqlConnection(constr))
{
using(SqlDataAdapter sda = new SqlDataAdapter(query,con))
{
//填写DataTable包含Table中的记录。
DataTable dt = new DataTable();
sda.Fill(dt);

//将默认项插入DataTable。
DataRow row = dt.NewRow();
row [0] ="" ;;
dt.Rows.InsertAt(row,0);

//将DataTable指定为DataSource。
cbRoles.DataSource = dt;
cbRoles.DisplayMember =" Role";
cbRoles.ValueMember =" Role" ;;
}
}
}
private void PopulateDataGridView()
{
string query =" SELECT EmpID,FirstName,LastName,Role,Grade,Dept ,从员工转移";
查询+ ="在哪里 LastName = @ LastName" ;;
query + =" AND Role = @Role)" ;; //在此处添加其他参数
query + ="或ISNULL(@ LastName,'')=''" ;;
string constr = @" Data Source = DESKTOP-Q2B3UUH\SQLEXPRESS; Initial Catalog = PeopleManager; Integrated Security = True" ;;
using(SqlConnection con = new SqlConnection(constr))
{
using(SqlCommand cmd = new SqlCommand(query,con))
{
cmd.Parameters。 AddWithValue(" @ LastName",cbLastName.SelectedValue);
cmd.Parameters.AddWithValue(" @ Role",cbRoles.SelectedValue);
using(SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
dgEmpList.DataSource = dt;
}
}
}
}


 顺便说一句,确实是将DataAccess直接放在您的表单中的最佳实践,但这是另一个主题的主题。


Hi all!

解决方案

Hi rockovo,

Shouldn't be too hard. After you've added the additional ComboBoxes to your Form, simply add the additional code, similar to what you already have for the Names. And change your PopulateDataGridView to add the additional parameters. I'll show you an example with adding one ComboBox for Roles:

 private void frmEmpList_Load(object sender, EventArgs e)
 {
     this.PopulateComboBoxes();
     this.PopulateDataGridView();
 }
 private void PopulateComboBoxes()
 {
     this.PopulateComboBoxLastName();
     this.PopulateComboBoxRoles();
 }
 private void PopulateComboBoxLastName()
 {
     // same code as you already have it ...
 }
 private void PopulateComboBoxRoles()
     string query = "SELECT DISTINCT Role FROM Employees"; // or whatever the correct SELECT would be
     string constr = @"Data Source=DESKTOP-Q2B3UUH\SQLEXPRESS;Initial Catalog=PeopleManager;Integrated Security=True";
     using (SqlConnection con = new SqlConnection(constr))
     {
         using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
         {
             //Fill the DataTable with records from Table.
             DataTable dt = new DataTable();
             sda.Fill(dt);

             //Insert the Default Item to DataTable.
             DataRow row = dt.NewRow();
             row[0] = "";
             dt.Rows.InsertAt(row, 0);

             //Assign DataTable as DataSource.
             cbRoles.DataSource = dt;
             cbRoles.DisplayMember = "Role";
             cbRoles.ValueMember = "Role";
         }
     }
 }            
 private void PopulateDataGridView()
 {
     string query = "SELECT EmpID, FirstName, LastName, Role, Grade, Dept, Shift FROM Employees";
     query += " WHERE (LastName = @LastName";
     query += "   AND  Role = @Role)"; // add the other parameters here
     query += " OR ISNULL(@LastName, '') = ''";
     string constr = @"Data Source=DESKTOP-Q2B3UUH\SQLEXPRESS;Initial Catalog=PeopleManager;Integrated Security=True";
     using (SqlConnection con = new SqlConnection(constr))
     {
         using (SqlCommand cmd = new SqlCommand(query, con))
         {
             cmd.Parameters.AddWithValue("@LastName", cbLastName.SelectedValue);
             cmd.Parameters.AddWithValue("@Role", cbRoles.SelectedValue);
             using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
             {
                 DataTable dt = new DataTable();
                 sda.Fill(dt);
                 dgEmpList.DataSource = dt;
             }
         }
     }
 }

 BTW, it's really not a Best Practice to have your DataAccess directly in your Form like this, but that's a topic for another thread.


这篇关于如何使用C#中的多个组合框过滤datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆