如何使用DataTable的Select方法来获取以“M”开头的UserNames。让我们假设UserNameTextBox有一个字符串 M
oDataTable.Select("UserName = '" + UserNameTextBox.Text + "'");
DataView oDataView = oDataTable.DefaultView;
oDataView.Sort = "UserName";
UserGridView.DataSource = oDataView;
UserGridView.DataBind();
当我在文本框中输入全名时,我确定DataTable包含它,它列出的不仅仅是我输入的内容。有人能帮帮我吗?
答案 0 :(得分:2)
您可以使用DataTable.Select
,DatView.RowFilter
方法或LINQ-to-DataSet
:
LINQ:
var tblFiltered = (from row in oDataTable.AsEnumerable()
where row.Field<String>("UserName").Contains(UserNameTextBox.Text)
select row).CopyToDataTable();
ADO.NET(DataTable.Select):
DataRow[] filteredRows = oDataTable.Select("UserName LIKE '%" + UserNameTextBox.Text + "%'");
ADO.NET(DataView.RowFilter):
oDataView.RowFilter = "UserName LIKE '%" + UserNameTextBox.Text + "%'";
答案 1 :(得分:1)
Select方法返回DataRow集合,但不会更改DataView,您可以创建DataView并将过滤字符串设置为DataView的RowFilter,请在此处查看:http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx
DataView view = new DataView();
view.Table = oDataTable;
view.RowFilter = "UserName = '" + UserNameTextBox.Text + "'";
UserGridView.DataSource = view;
UserGridView.DataBind();