如何检查DataView中是否已存在记录? C#

时间:2010-11-19 04:36:59

标签: c#

我有一个DataView,它有两列:ContactIDName

如何检查DataView中是否已存在特定的ContactID

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

DataView有一个名为FindRows的方法,可用于搜索特定的contactID,例如......

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

答案 2 :(得分:0)

使用以下代码查找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");
        }