我有一個應用程序顯示給定目錄中的所有文件。現在我想在應用程序中實現搜索功能。我使用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+"'");
}
}
}
什麼「不起作用」。我不認爲太多的人想花時間篩選代碼,如果你沒有具體解決這個問題。 – 2012-03-30 02:39:01
抱歉。我的意思正是我所說的過濾器不起作用。即使我在文本框中輸入值,它也不會過濾。 – 2012-03-30 02:42:00