过滤我的Datagridview以获取文本框文本

时间:2015-08-19 21:25:46

标签: c# visual-studio datagridview filter

我实际上是在尝试从特定列的文本框中过滤我的Datagridview中的文本。问题是,我从另一个线程中选择了Code,但它一直告诉我Datagridview的Datasource是'Null'

我的Datagridview代码

private void Icon_Picker_Load(object sender, EventArgs e)
{
    string[] first = {"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"};

    dataGridView1.Rows.Clear();
    dataGridView1.Refresh();
    //image directory
    DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Daniel\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\images");

    //getting all files from the Directory
    foreach(FileInfo file in dir.GetFiles())
    {

        try
        {
            this.img16x16.Images.Add(Image.FromFile(file.FullName));
            this.img32x32.Images.Add(Image.FromFile(file.FullName));


            for (int j = 0; j < img16x16.Images.Count; j++)
            {
                var row = new DataGridViewRow();

                dataGridView1.Rows.Add(row);
                dataGridView1.Rows[j].Cells[0].Value = first[j];

                //Resizing icons to 16x16 icons
                img16x16.ImageSize = new Size(16, 16);

                //adding icons to the datagridview
                dataGridView1.Rows[j].Cells[1].Value = img16x16.Images[j];

                //Resizing icons to 32x32 icons
                img32x32.ImageSize = new Size(32, 32);

                //adding icons to the datagridview
                dataGridView1.Rows[j].Cells[2].Value = img32x32.Images[j];


            }

        }catch
        {
            Console.WriteLine("This is not an image file");
        }
    }

我的TextChanged功能

 private void txtFilter_TextChanged(object sender, EventArgs e)
    {

         (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", txtFilter.Text);
    }

感谢您的帮助。

0 个答案:

没有答案