auto search filter without convert to DataTable
HI i have a textbox and a gridview. May i know what is the best way to have something type in the textbox and it auto filter the gridview based on a keyword e.g. Name
i have done that successfuly but with the condition only if it converted to dataTable.
bsCourse.DataSource = linqHelper.ToDataTable(course.Get() as List<Course>);
gvCourse.DataSource = bsCourse;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
string input = txtSearch.Text.Trim().ToLower();
if (input.Length > 0)
bsCourse.Filter = "Name lik开发者_Python百科e '" + input + "%'";
else
bsCourse.Filter = "";
}
Is there a better without converting the data to the datatable for this auto filter search? i am using linq to sql. Thanks
I usually filter out which rows to hide by using regular expressions. In this example I've used an UltraGrid (Infragistics), but the procedure would probably be something along the same lines for a gridview.
private void searchBox_TextChanged(object sender, EventArgs e)
{
if (gridUsers.Rows.Count > 0)
{
foreach (UltraGridRow row in gridUsers.Rows)
{
if (Regex.IsMatch(row.Cells[1].Value.ToString(), searchBox.Text, RegexOptions.IgnoreCase))
{
gridUsers.Rows[indexCounter].Hidden = false;
}
else
{
gridUsers.Rows[indexCounter].Hidden = true;
}
}
}
}
It might not be the best way (especially not for very large grids with a lot of rows), but it works for me.
You can try the Linq approach, this way:
List<Course> courses = (from i in course.Get()
select new Course() {
// Set your property value here.
}).ToList();
bsCourse.DataSource = courses;
精彩评论