如何检查数据视图中是否存在记录? C#

问题描述:

我已经有两个列的数据视图:ContactIDName如何检查数据视图中是否存在记录? C#

我如何检查特定ContactID已经在数据视图现有的?

+0

我无法访问msdn ... – yonan2236 2010-11-19 04:42:56

+0

等等,说什么?你可以访问任何其他网站?搜索我提供的方法名称。 – 2010-11-19 04:43:23

+0

是的......我不知道为什么......有没有用linquish的方法来做到这一点? – yonan2236 2010-11-19 04:46:04

的数据视图了,这可用于搜索特定的ContactID,例如一个名为FindRows方法...

var table = new DataTable(); 
var column = new DataColumn("Id", typeof (int)); 

table.Columns.Add(column); 
table.PrimaryKey = new[] {column}; // Unique Constraint 

var row = table.NewRow(); 
row["Id"] = 100; 
table.Rows.Add(row); 

row = table.NewRow(); 
row["Id"] = 200; 
table.Rows.Add(row); 

var view = new DataView(table) { ApplyDefaultSort = true }; 
var rows = view.FindRows(200); 

foreach(var r in rows) 
{ 
    Console.WriteLine(r["Id"]); 
} 

使用以下代码在Dataview中查找行

 //Your original Table Which consist of Data 
     DataTable dtProducts = new DataTable(); 
     //Add the DataTable to DataView 
     DataView ProductDataView = new DataView(dtProducts); 
     ProductDataView.RowFilter = ""; 
     ProductDataView.Sort = "ProdId"; 
     int recordIndex = -1; 
     //In the Find Row Method pass the Column 
     //value which you want to find 
     recordIndex = ProductDataView.Find(1); 
     if (recordIndex > -1) 
     { 
      Console.WriteLine("Row Found"); 
     }