Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got an application which display all files in a given directory. And now I want to implement a search function within the application. I use the textBox_textChanged method to implement the search as it is faster. But somehow I cannot get it to work. It won't filter the datagridview even when values is entered into the textbox. I don't know what's the problem. Here is my code :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace MonitorDirectory
{
    public partial class Form1 : Form
    {   private Timer timer;
        private int count;
        DataTable dt = new DataTable();
        DataRow dr;
        String[] s1;
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {   
            count = 0;
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer1_Tick);
            timer.Start();
            s1 = Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\FILE","*.*",SearchOption.AllDirectories);
            for (int i = 0; i <= s1.Length - 1; i++)
            {
                if (i == 0)
                {
                    dt.Columns.Add("File_Name");
                    dt.Columns.Add("File_Type");
                    dt.Columns.Add("File_Size");
                    dt.Columns.Add("Create_Date");
                }

                //Get each file information
                FileInfo info = new FileInfo(s1[i]);
                FileSystemInfo sysInfo = new FileInfo(s1[i]);
                dr = dt.NewRow();
                //Get File name of each file name
                dr["File_Name"] = sysInfo.Name;
                //Get File Type/Extension of each file 
                dr["File_Type"] = sysInfo.Extension;
                //Get File Size of each file in KB format
                dr["File_Size"] = (info.Length / 1024).ToString();
                //Get file Create Date and Time 
                dr["Create_Date"] = sysInfo.CreationTime.Date.ToString("dd/MM/yyyy");
                //Insert collected file details in Datatable
                dt.Rows.Add(dr);
                //
              
                
                if ((info.Length / 1024) > 5000)
                {
                   MessageBox.Show("" + sysInfo.Name + " had reach its size limit.");
                }
            }
            if (dt.Rows.Count > 0)
            {
                //Finally Add DataTable into DataGridView
                dataGridView1.DataSource = dt;
            } 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                count++;
                if (count == 300)
                {
                    count = 0;
                    timer.Stop();
                    Application.Restart();
                }
        }
        public string secondsToTime(int seconds)
        {
             int minutes = 0;
             int hours = 0;

             while (seconds >= 60)
             {
                minutes += 1;
                seconds -= 60;
             }
             while (minutes >= 60)
             {
                hours += 1;
                minutes -= 60;
             }

             string strHours = hours.ToString();
             string strMinutes = minutes.ToString();
             string strSeconds = seconds.ToString();

             if (strHours.Length < 2)
                 strHours = "0" + strHours;
             if (strMinutes.Length < 2)
                 strMinutes = "0" + strMinutes;
             if (strSeconds.Length < 2)
                 strSeconds = "0" + strSeconds;
             return strHours + ":" + strMinutes + ":" + strSeconds;
         }

        //This is the filtering part of the program.
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataRow[] dr = dt.Select("File_Name like '%" + textBox1.Text + "%'");
        }
   }
}
Posted

1 solution

Use a BindingSource to bind the data to the DataGridView and set the Filter property of the BindingSource in the TextChanged event as below
C#
//in the Load event of Form
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = dt;

//Handle the TextChanged event
private void textBox1_TextChanged(object sender, EventArgs e)
{
   bindingSource1.Filter = string.Format("File_Name like '%{0}%'",textBox1.Text.Trim());
}
 
Share this answer
 
v2
Comments
nizam15 30-Mar-12 0:10am    
Thanks it works. But I was wondering why mine isn't working. Can we not filter using data table??
ProEnggSoft 30-Mar-12 0:14am    
If you want to filter the DataTable, use the DefaulView property of DataTable to set the DataSource and then set the RowFilter property of the DataView.
If your problem is solved, you may consider to vote and accept the solution. Thank you.
member60 30-Mar-12 1:31am    
my 5!
ProEnggSoft 30-Mar-12 1:57am    
Thank you.
nizam15 30-Mar-12 2:37am    
Understood. Thanks again man. 5!:D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900