0

我已经创建并返回了数据表,这个表有 10 列。现在我想根据一些动态搜索参数从这个表中过滤。这该怎么做?任何想法都会得到及时的帮助。

// This function will create and return the source table.
var DisplayTable = CreateQueryTable(); 

在这里我想做动态搜索If col1=MyName and Col2=MyCity

ResultGrid.DataSource = DisplayTable;
ResultGrid.DataBind();
Panel1.Controls.Add(ResultGrid);
4

2 回答 2

6

你可以通过这些方式做到这一点,

1.创建数据视图喜欢

var dv = dataTable.DefaultView;
dv.RowFilter =  "col1='MyName' and Col2='MyCity'"; // if MyName and MyCity are literal string.

或者

dv.RowFilter = "col1='"+MyName+"' and Col2 ='"+ MyCity +"'";// if MyName and MyCity are string variable.

2.使用DataTable Select方法,它会返回DataRow数组

var rows = dataTable.Select("col1='MyName' and Col2='MyCity'"); //if string literal

或者

var rows = dataTable.Select("col1='"+MyName+"' and Col2='"+MyCity+"'"); // if string variable

3.林奇

var filterdData = from row in dataTable.AsEnumerable()
                  where row.Field<string>("col1") == "MyName" 
                  && row.Field<string>("col2") == "MyCity"
                  select row;
于 2012-08-21T09:14:33.823 回答
2

您创建数据表的 DataView 并使用 Filter // Create a DataView DataView dv = new DataView(yourDataTable); dv.RowFilter = "col1='MyName' and Col2='MyCity'"; //用DataView绑定你的网格

你也可以在你的桌子上使用 select 方法

  DataRow[] foundRows;
  foundRows = yourDataTable.Select("col1='MyName' and Col2='MyCity'");

您还可以使用 Linq To DataTable

var results = from myRow in yourDataTable.AsEnumerable()
where myRow.Field<string>("col1") == Myname &&
      myRow.Field<string>("Col2") == MyCity
select myRow;
于 2012-08-21T09:07:12.330 回答