簡體   English   中英

如何在數據視圖中過濾數據

[英]How to filter data in dataview

我想在 listview 的 textchange 事件上過濾數據,所以我使用 dataview 來過濾數據。 下面代碼中的問題是,我在每個內部使用數據視圖,以便它只檢查一個條件,它只需要最后一個值,我想用數據視圖檢查s1中的值,剩余的值應該與列表視圖綁定。

例如:如果我在文本框中鍵入一個文本框,它應該列出所有以 anandha kumar、anna 等值開頭的項目值。 假設我將值 anandha kumar 和 anna 保存在數組 s1 中。 我應該在列表視圖中列出所有其他值,期望像 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();

        }
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 '" + serachText + "*') AND (CODE <> '" + s1 + "')";
                    foreach(DataRow dr in dvProducts.ToTable().Rows)
                    {
                       newTable.ImportRow(dr);
                    }
                }
            }
ListView1.DataSource = newTable;
ListView1.DataBind();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM