【问题标题】:Datagrid is not filtering数据网格没有过滤
【发布时间】:2016-07-23 23:10:19
【问题描述】:

我有一个使用 loadStudentTable() 方法填充表格数据的数据网格。

我有一个搜索框,我在其中尝试使用包含的值过滤 dataGrid。

它似乎没有工作。说明错误:

对象引用未设置为对象的实例。

搜索文本更改

private void SearchTxt_TextChanged(object sender, EventArgs e)
        {
            try 
            { 
                (studentGridView.DataSource as DataTable).DefaultView.RowFilter = string.Format("Student_FName LIKE '%{0}%'", SearchTxt.Text);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

学生桌负荷

 //Fills out Student table
    private void loadStudentTable()
    {
        SqlConnection conn2 = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
        conn2.Open();

        try
            {
                SqlCommand cmdDatabase2 = new SqlCommand("Select * from Student", conn2);
                SqlDataAdapter sda2 = new SqlDataAdapter();
                sda2.SelectCommand = cmdDatabase2;
                DataTable dbdataset2 = new DataTable();
                sda2.Fill(dbdataset2);
                BindingSource bSource2 = new BindingSource();

                bSource2.DataSource = dbdataset2;
                studentGridView.DataSource = bSource2;
                sda2.Update(dbdataset2);

                studentGridView.Columns[0].Width = 92;
                studentGridView.Columns[1].Width = 200;
                studentGridView.Columns[2].Width = 180;
                studentGridView.Columns[3].Width = 180;
                studentGridView.Columns[4].Width = 170;
                studentGridView.Columns[5].Width = 170;
                studentGridView.Columns[6].Width = 130;                        
            }
        catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        conn2.Close();
    }

加载表格很好,它只是过滤搜索文本框中使用的输入。

请有任何想法或帮助。

【问题讨论】:

  • 不要打电话给DataGridViewa GridViewDataGrid!!这是错误的和令人困惑的......总是用他们的正确名字来称呼事物!是的,需要多输入 四个 个字母。..
  • @TaW 对不起,我下次会。随意编辑它或让我编辑,我会的。

标签: c# mysql visual-studio datagridview


【解决方案1】:

如下拆分你的代码并尝试..

private void SearchTxt_TextChanged(object sender, EventArgs e)
    {
        try 
        { 

        var bindData = (BindingSource)studentGridView.DataSource;
        var dataTable = (DataTable)bindData.DataSource;
        dataTable.DefaultView.RowFilter = string.Format(""Student_FName LIKE '%{0}%'", SearchTxt.Text);    
        studentGridView.Refresh();

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

【讨论】:

  • 只是一个简单的问题,如果我想使用相同的查询来查找 Student_Username 或 Student_SName。我会在哪里改变,我应该添加什么?因为我想尽量减少代码重用
  • 嗨@DB,在上面的代码中,您正在搜索 Student_FName 列,如果您想基于另一列执行搜索,只需更改列名,如下面的字符串。 Format(""Student_Username LIKE '%{0}%'", SearchTxt.Text)。并且,如果您缩进代码重用,请创建一个通用类并创建一个全局数据表对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多