C# 如何过滤数据视图中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10009675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:07:03  来源:igfitidea点击:

How to filter data in dataview

c#asp.net

提问by Sambath Kumar

I want to filter data on the textchange event on listview so I use dataview to filter data. Issue in the below code is, I use dataview inside for each so that it checks only one condition that is last value only it takes, I want to check value in s1with dataview and remaining value should bind with listview.

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

eg: if I type anin textbox it should list all the item values starting with an value like anandha kumar,anna ect. suppose I keep the value anandha kumar and anna in array s1. I should list all other values expect the array values like antony ect... in listview.

例如:如果我在文本框中键入an,它应该列出所有以 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();

        }

回答by las

Eg:

例如:

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

回答by Balachandar P

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

Ref: http://www.csharp-examples.net/dataview-rowfilter/

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

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

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