2012-04-04 79 views
8

我想過濾listview上textchange事件的數據,所以我使用dataview來過濾數據。下面代碼中的問題是,我在每個數據視圖中使用dataview,以便它只檢查一個條件,它僅僅是最後一個值,我想用dataview檢查s1中的值,剩餘值應該與listview綁定。如何過濾dataview中的數據

例如:如果我在文本框中鍵入應該列出所有開始像anandha庫馬爾的值項目值,安娜等。假設我在數組s1中保留anandha kumar和anna的值。我應該列出所有其他值,例如列表視圖中的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(); 

     } 
+0

你想連接的結果,並要顯示的最終輸出? – Pankaj 2012-04-04 11:20:38

回答

5

如:

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

試試上面的代碼。 – las 2012-04-04 11:08:42

+0

檢查根據你自己的,http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.items.aspx – las 2012-04-04 11:14:38

+0

也得到改寫這個鏈接和格式列表視圖。我想省略數據視圖S1值,並存儲在某個表 – 2012-04-04 14:10:25

13
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

+0

我給view.RowStateFilter = DataViewRowState.ModifiedCurrent;我顯示空的數據視圖。我想省略dataview中的s1值 – 2012-04-04 14:07:14

+0

view.RowFilter =「City!='Berlin'」; – 2012-04-06 12:08:57