使用数据表进行问题过滤 [英] Problem filtering using a datatable
本文介绍了使用数据表进行问题过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个显示给定目录中所有文件的应用程序.现在,我想在应用程序中实现搜索功能.我使用textBox_textChanged方法来实现搜索,因为它更快.但是以某种方式,我无法使其正常工作.即使在文本框中输入值,它也不会过滤datagridview.我不知道出什么问题了.这是我的代码:
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 + "%'");
}
}
}
推荐答案
使用BindingSource
将数据绑定到DataGridView
,并在其中设置BindingSource
的Filter
属性.TextChanged
事件如下
Use aBindingSource
to bind the data to theDataGridView
and set theFilter
property of theBindingSource
in theTextChanged
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());
}
这篇关于使用数据表进行问题过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文