2012-09-28 81 views
-4

我想從數據集列Id中找到值。 這裏是集從數據集的列中找到一個值asp.net

Id Value 
1 football 
2 Tennis 
3 Cricket 

如果任何一個在塔缺席的話,我想追加數據集中

+0

列中缺少什麼意思? – Cdeez

+0

如果id = 1或2或3不存在,那麼我需要添加特定的行與數據集 –

回答

0

特定值我想這是一個DataSet中的DataTable。首先,你需要查詢,如果該ID是在數據表:

var dataTable = dataSet.Tables[0]; //For this example I'm just getting the first DataTable of the DataSet, but it could be other. 
var id = 1; 
var value = "football"; 

//Any(...) will return true if any record matches the expression. In this case, the expression is if a Id Field of the row is equals to the provided id 
var contained = dataTable.AsEnumerable().Any(x =>x.Field<int>("Id") == id); 

然後,如果它不存在,添加一個新行:

if(!contained) 
{ 
    var row = dataTable.NewRow(); 

    row["Id"] = id; 
    row["Value"] = value; 

    dataTable.Rows.Add(row); 
} 

希望它可以幫助

0

首先,你應該使用一個循環來查看你的數據集列'id'是否包含該值。如果該ID不存在,則:

DataRow newrow = ds.Tables[0].NewRow(); //assuming ds is your dataset 
    newrow["id"] = "your new id value"; 
    newrow["value"] = "your new value"; 
    ds.Tables[0].Rows.Add(newrow); 
相關問題