Click here to Skip to main content
Sign Up to vote bad
good
See more: C#3.0
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 :
 
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 29 Mar '12 - 17:10
nizam15527


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
//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());
}
  Permalink  
Comments
msnizam - 30 Mar '12 - 0:10
Thanks it works. But I was wondering why mine isn't working. Can we not filter using data table??
ProEnggSoft - 30 Mar '12 - 0:14
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:31
my 5!
ProEnggSoft - 30 Mar '12 - 1:57
Thank you.
msnizam - 30 Mar '12 - 2:37
Understood. Thanks again man. 5!:D
ProEnggSoft - 30 Mar '12 - 9:09
Thanks.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 206
1 Sergey Alexandrovich Kryukov 175
2 Richard MacCutchan 145
3 Tadit Dash 140
4 Santhosh G_ 125
0 Sergey Alexandrovich Kryukov 10,294
1 OriginalGriff 7,955
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 30 Mar 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid