如何在dataview中过滤数据

时间:2012-04-04 10:53:59

标签: c# asp.net

我想在listview上过滤textchange事件的数据,所以我使用dataview来过滤数据。下面的代码中的问题是,我在每个内部使用dataview,以便它只检查一个条件,它只是最后一个值,我想用数据视图检查 s1 中的值,剩下的值应该与列表视图。

例如:如果我在文本框中键入一个,它应列出所有项目值,以anandha kumar,anna等值开头。假设我在数组s1中保留anandha kumar和anna的值。我应该列出所有其他值,期望像listview中的antony ect ...这样的数组值。

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();

        }

2 个答案:

答案 0 :(得分:17)

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

答案 1 :(得分:7)

例如:

Datatable newTable =  new DataTable();

            foreach(string s1 in list)
            {
                if (s1 != string.Empty) {
                    dvProducts.RowFilter = "(CODE like '" + serachText + "*') AND (CODE <> '" + s1 + "')";
                    foreach(DataRow dr in dvProducts.ToTable().Rows)
                    {
                       newTable.ImportRow(dr);
                    }
                }
            }
ListView1.DataSource = newTable;
ListView1.DataBind();