Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 18:10pm
nizam15571

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
msnizam at 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 at 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 at 30-Mar-12 1:31am
   
my 5!
ProEnggSoft at 30-Mar-12 1:57am
   
Thank you.
msnizam at 30-Mar-12 2:37am
   
Understood. Thanks again man. 5!:D
ProEnggSoft at 30-Mar-12 9:09am
   
Thanks.

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

  Print Answers RSS
0 OriginalGriff 484
1 Peter Leow 443
2 Sergey Alexandrovich Kryukov 288
3 Kornfeld Eliyahu Peter 270
4 Maciej Los 230


Advertise | Privacy | Mobile
Web04 | 2.8.150301.1 | Last Updated 30 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100