DataTable.Select
I have a DataTable with a column called Name.
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Name";
dt.Columns.Add(dc);
I'm trying to select only the DataRow开发者_如何学运维s that have what's in the TextBox, but I don't know how to . Here's what I want.
dt.Select("string.Compare(Name.ToLower().Contains(" + textBox1.Text.ToLower() + ")");
Is there any way to do this with Select / should I even be attempting it?
You can use LINQ to Dataset to do this (via AsEnumerable):
var results = dt.AsEnumerable().Where(dr => dr.Field<string>("Name").ToLower().Contains(textBox1.Text.ToLower()));
I don't believe the syntax of the expression you are using is valid. There a description of the valid syntax here. To get a contains
style operation with the dt.Select
, you could use the LIKE
operator. Also you would need to set the case sensitivity to false:
dt.CaseSensitive = false;
dt.Select("Name LIKE '%" + textBox1.Text + "%'");
you can use select
dt.select("Name = '"+textbox.text.trim()+"'");
if you want to select something similar to the textbaox then you like
dt.select("Name LIKE '%"+textbox.text.trim()+"%'");
精彩评论