【问题标题】:How to filter datagridview based on combobox selection如何根据组合框选择过滤datagridview
【发布时间】:2018-08-10 07:35:19
【问题描述】:

我有一个DatagridView 包含学生信息,包括班级、年龄等。现在我想创建一个具有过滤哪个班级的功能的组合框(例如,如果我在组合框上选择 IIA 类,则仅限 datagridview展示 IIA 班的学生)。

这是我在表单中从objectbindingsource 加载数据的代码:

private void frmdbSiswa_Load(object sender, EventArgs e)
{
    db = new SiswaSMSEntities();
    tabelSiswaBindingSource.DataSource = db.Tabel_Siswa.ToList();
    agamaSiswaBindingSource.DataSource = db.Agama_Siswa.ToList();
    kelasBindingSource.DataSource = db.Kelas.ToList();
    jenisKelaminBindingSource.DataSource = db.Jenis_Kelamin.ToList();

    dataGridViewSiswa.DataSource = db.Tabel_Siswa.ToList();//to show the datagridview
    cboKelas.DataSource = db.Kelas.ToList();//combobox
}

这里是组合框的代码:

private void cboKelas_SelectionChangeCommitted(object sender, EventArgs e)
{
    dataGridViewSiswa.DataSource = db.Tabel_Siswa.Where(x => x.IdKelas == cboKelas.SelectedIndex).ToList();
}

我还将组合框绑定到数据源。 我为此工作了好几个小时。我是编程新手,所以如果我问一个非常基本的问题,请原谅我。

问题是,当我运行代码时,它会过滤数据,但是当我选择 IA 类时,datagridview 什么也没有显示,当我选择 IB 类时,datagridview 显示来自 IA 类的学生等等。还有当我选择datagridview时,组合框显示system.Data.Entity.Error

【问题讨论】:

  • 不确定你的意思现在我想创建一个具有过滤DatagridView上显示的数据的功能的组合框
  • @RahulAgarwal 我已经编辑了问题
  • 那么您在使用这段代码时遇到了什么问题?
  • 感谢您的回复@ChetanRanpariya 我已编辑问题
  • 这能回答你的问题吗? How to filter the DataGridView using ComboBox

标签: c# sql-server datagridview filter combobox


【解决方案1】:

在组合框选择更改事件时,将数据网格视图 RowFilter 属性设置为选定值:

dv.RowFilter = "YourColumnName = ComboBoxSelectedValue";

用类似的问题/解决方案更新这个here

【讨论】:

  • 我已经添加了代码,但它不起作用@RahulAgarwal
  • @OktavianusMisro 以确保我正确理解了您的问题,请告诉我一件事 - 您要查找的内容与询问 here 不同?
  • 对不起,我误解了你的回答嘿嘿,我去试试这个
【解决方案2】:

在页面加载中替换这个添加 !IsPostBack

private void frmdbSiswa_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
 {
    db = new SiswaSMSEntities();
    tabelSiswaBindingSource.DataSource = db.Tabel_Siswa.ToList();
    agamaSiswaBindingSource.DataSource = db.Agama_Siswa.ToList();
    kelasBindingSource.DataSource = db.Kelas.ToList();
    jenisKelaminBindingSource.DataSource = db.Jenis_Kelamin.ToList();

    dataGridViewSiswa.DataSource = db.Tabel_Siswa.ToList();//to show the datagridview
    dataGridViewSiswa.DataBind();
    cboKelas.DataSource = db.Kelas.ToList();//combobox
    cboKelas.DataBind();
   }
}

private void cboKelas_SelectionChangeCommitted(object sender, EventArgs e)
{
    dataGridViewSiswa.DataSource = db.Tabel_Siswa.Where(x => x.IdKelas == cboKelas.SelectedIndex).ToList();
 dataGridViewSiswa.DataBind();

}

【讨论】:

  • 感谢您的快速响应,@NegiRox 但是,它不起作用
【解决方案3】:

你可以这样试试吗?

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].Name = "Product ID";
            dataGridView1.Columns[1].Name = "Product Name";
            dataGridView1.Columns[2].Name = "Product Price";

            string[] row = new string[] { "1", "Product 1", "1000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "2", "Product 2", "2000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "3", "Product 3", "3000" };
            dataGridView1.Rows.Add(row);
            row = new string[] { "4", "Product 4", "4000" };
            dataGridView1.Rows.Add(row);

            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.HeaderText = "Select Data";
            cmb.Name = "cmb";
            cmb.MaxDropDownItems = 4;
            cmb.Items.Add("True");
            cmb.Items.Add("False");
            dataGridView1.Columns.Add(cmb);

        }
    }
}

如果您还有其他问题,请回复。

http://csharp.net-informations.com/datagridview/csharp-datagridview-combobox.htm

【讨论】:

    【解决方案4】:

    除非您绝对必须将List<T> 用作网格的DataSource,否则我建议您将DataTable 用作网格的DataSource。这可能会使事情变得更容易,而且您可能已经拥有来自db.Tabel_Siswa.ToList()DataTable,它似乎正在从表格中列出。

    话虽如此,下面是一个使用组合框通过DataTableDataView 过滤网格中数据的示例。这是一个简单的表单,有一个空的DataGridView 和一个空的ComboBox。全局DataTable 变量gridTable 用作网格的DataSource。全局DataView 变量dv 用于“过滤/取消过滤”gridTable

    在表单加载时,列和一些测试数据被添加到gridTable。然后将gridTable 设置为DataSource 到网格。最后,ComboBox 填充了“A 类、B 类”等项……组合框中添加了“无过滤器”选项,以允许用户删除应用的过滤器。

    当用户更改组合框选择时,会创建一个新的DataView,并将其RowFilter 设置为组合框中的选定值。最后将DataViewdv设置为DataSource到网格中显示过滤结果

    DataTable gridTable;
    DataView dv;
    
    public Form1() {
      InitializeComponent();
    }
    
    private void Form1_Load(object sender, EventArgs e) {
      gridTable = GetTable();
      FillTable(gridTable);
      dataGridView1.DataSource = gridTable;
      SetComboBox();
    }
    
    private void SetComboBox() {
      comboBox1.Items.Add("No Filter");
      comboBox1.Items.Add("Class A");
      comboBox1.Items.Add("Class B");
      comboBox1.Items.Add("Class C");
      comboBox1.SelectedIndex = 0;
    }
    
    private DataTable GetTable() {
      DataTable dt = new DataTable();
      dt.Columns.Add("FName", typeof(string));
      dt.Columns.Add("LName", typeof(string));
      dt.Columns.Add("Class", typeof(string));
      return dt;
    }
    
    private void FillTable(DataTable dt) {
      for (int i = 1; i < 5; i++) {
        dt.Rows.Add("FName" + i, "LName" + i, "Class A");
        dt.Rows.Add("FName" + i, "LName" + i, "Class B");
        dt.Rows.Add("Class" + i, "Class" + i, "Class C");
      }
    }
    

    RowFilter 字符串…dv.RowFilter = "Class = '" + comboBox1.Text + "'" 表示要过滤,以使名为“类”的列中的单元格等于组合框中的文本。示例:“类 = B 类”。这意味着DataSource 的列名“匹配”过滤器字符串中的内容。

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
      dv = new DataView(gridTable);
      if (comboBox1.Text == "No Filter")
        dv.RowFilter = "";
      else
        dv.RowFilter = "Class = '" + comboBox1.Text + "'";
      dataGridView1.DataSource = dv;
    }
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢你,我要试试这个@JohnG。看来这将我引向了正确的方向。
    • 我需要删除这个 tabelSiswaBindingSource.DataSource = db.Tabel_Siswa.ToList(); agamaSiswaBindingSource.DataSource = db.Agama_Siswa.ToList(); kelasBindingSource.DataSource = db.Kelas.ToList(); jenisKelaminBindingSource.DataSource = db.Jenis_Kelamin.ToList(); @JohnG
    • 我猜删除可能不是最好的主意。在不知道 SiswaSMSEntities(); 返回什么的情况下,删除 .ToList() 可能会起作用。我从 db.Tabel_Siswa 这个名字猜想这是一个 DataTable。此外,我的代码使用的是DataTable,看来您可能使用的是BindingSource。此数据表可能仍适用于BindingSource,但您很可能必须使用BindingSources“过滤器”属性。最后,对于组合框,您应该使用 SelectedIndexChanged 事件而不是 SelectionChangeCommitted 事件。
    • 好的,谢谢你的好意,我正在努力。如果它有效,我会投票给你的答案。谢谢@JohnG
    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多