Guest User

Untitled

a guest
Jan 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. Id Value
  2. 1 football
  3. 2 Tennis
  4. 3 Cricket
  5.  
  6. var dataTable = dataSet.Tables[0]; //For this example I'm just getting the first DataTable of the DataSet, but it could be other.
  7. var id = 1;
  8. var value = "football";
  9.  
  10. //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
  11. var contained = dataTable.AsEnumerable().Any(x =>x.Field<int>("Id") == id);
  12.  
  13. if(!contained)
  14. {
  15. var row = dataTable.NewRow();
  16.  
  17. row["Id"] = id;
  18. row["Value"] = value;
  19.  
  20. dataTable.Rows.Add(row);
  21. }
  22.  
  23. DataRow newrow = ds.Tables[0].NewRow(); //assuming ds is your dataset
  24. newrow["id"] = "your new id value";
  25. newrow["value"] = "your new value";
  26. ds.Tables[0].Rows.Add(newrow);
Add Comment
Please, Sign In to add comment