如何在数据视图中筛选数据

本文关键字:数据 筛选 视图 | 更新日期: 2023-09-27 18:29:54

我想过滤listview上textchange事件的数据,所以我使用dataview来过滤数据。下面代码中的问题是,我在每个代码中使用dataview,这样它只检查一个条件,该条件是它需要的最后一个值,我想用dataview检查s1中的值,其余值应该与listview绑定。

例如:如果我在文本框中键入一个,它应该列出所有以anandha kumar、anna等值开头的项值。假设我在数组s1中保留值anandha kumar和anna。我应该列出所有其他值,除了数组值,比如antony等等。。。在列表视图中。

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            dvProducts = (DataView)Session["ListViewItems"];
            string serachText = EscapeLikeValue(TextBox1.Text);
            string lvValues = hdRetailCustomerGroup.Value;
            string trim = lvValues.Replace(" ", "");
            trim = trim.Replace("'r", "");
            trim = trim.Replace("'n", "");
            trim = trim.Replace("'t", "");
             string str = trim;
            string[] list = str.Split('|');

            foreach (string s1 in list)
            {
                if (s1 != string.Empty)
                {
                    dvProducts.RowFilter = "(CODE like '" + serachText + "*') AND (CODE <> '" + s1 + "')";
                    Session["ListViewItems"] = dvProducts;
                }
            }
                       ListView1.DataSource = dvProducts;
                     ListView1.DataBind();
        }

如何在数据视图中筛选数据

DataView view = new DataView();
view.Table = DataSet1.Tables["Suppliers"];
view.RowFilter = "City = 'Berlin'";
view.RowStateFilter = DataViewRowState.ModifiedCurrent;
view.Sort = "CompanyName DESC";
// Simple-bind to a TextBox control
Text1.DataBindings.Add("Text", view, "CompanyName");

参考编号:http://www.csharp-examples.net/dataview-rowfilter/

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

例如:

Datatable newTable = new DataTable();
foreach (string s1 in list)
{
   if (s1 != string.Empty)
   {
      dvProducts.RowFilter = "(CODE like '" + searchText + "*') AND (CODE <> '" + s1 + "')";
      foreach (DataRow dr in dvProducts.ToTable().Rows)
      {
         newTable.ImportRow(dr);
      }
   }
}
ListView1.DataSource = newTable;
ListView1.DataBind();
相关文章: