2012-03-30 78 views
1

我有一個應用程序顯示給定目錄中的所有文件。現在我想在應用程序中實現搜索功能。我使用textBox_textChanged方法來實現搜索,因爲它更快。但不知何故,我無法實現它的工作。我不知道有什麼問題。這裏是我的代碼:無法過濾數據表?

{ 
    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 filter code fragment. 
     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
      DataRow[] select = dt.Select("File_Name = '" + textBox1.Text+"'"); 
     } 
    } 
} 
+1

什麼「不起作用」。我不認爲太多的人想花時間篩選代碼,如果你沒有具體解決這個問題。 – 2012-03-30 02:39:01

+0

抱歉。我的意思正是我所說的過濾器不起作用。即使我在文本框中輸入值,它也不會過濾。 – 2012-03-30 02:42:00

回答

1

如果您正在使用框TextChanged,我假設你想匹配在哪裏您鍵入的內容將匹配您的輸入包含在任何文件名部分查找例如,如果鍵入「他「它將匹配‘幫助’,‘HelloWorld’的,等等

編輯:

而不是直接綁定到您的數據表,你應該使用BindingSource,因爲它會爲您提供一個過濾功能。

public BindingSource bindingSource; 

然後,更改此代碼:

if (dt.Rows.Count > 0) 
{ 
    //Finally Add DataTable into DataGridView 
    dataGridView1.DataSource = dt; 
} 

要這樣:

if (dt.Rows.Count > 0) 
{ 
    //Finally Add DataTable into DataGridView 
    bindingSource = new BindingSource(); 
    bindingSource.DataSource = dt; 
    dataGridView1.DataSource = bindingSource; 
} 

最後改變你的TextChanged事件處理程序,以這個來做實際的過濾:

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    bindingSource.Filter = string.Format("File_Name LIKE '%{0}%'", textBox1.Text); 
} 
+0

謝謝@BradRem。我試過了,但它仍然無效。我的gridview仍然顯示所有的文件,即使我在文本框中輸入值。 – 2012-03-30 02:45:01

+0

@ shahrul1509,我沒有看到你實際上在過濾你的gridView。選擇可能是工作,但它沒有連接到你的網格! – 2012-03-30 12:26:31

+0

@ shahrul1509,更新了代碼以顯示如何按照您的請求過濾您的datagridview。 – 2012-03-30 16:58:14