简体   繁体   中英

How to insert a value to a particular row in a DataTable C#

Please help me with a code snippet to insert a value to a particular row which already exists in my DataTable .

DataRow myrow;
for (i = 1; i <= cnt2+1; i++)
{
    myrow = finalRprt.NewRow();
    for (j = 1; j <= lstSubName.Count + 4; j++) 
    {
       myrow[j] = "-";
    }
    finalRprt.Rows.Add(myrow);
}

Now I want to add a value to the particular row depending on a condition. How can I do

This question is quite vague. But you can for example use the DataRow.SetField extension methid to set a field of a DataRow .

You can either use the indexer to get a row of a DataTable or use Linq-To-DataTable to find the rows you're searching for.

DataRow row = table.AsEnumerable()
                   .Where(r => r.Field<int>("ID")==ID)
                   .Single();
row.SetField("Name", newName);

Generic Field and SetField Methods (LINQ to DataSet)

Now you need a DataAdapter to update your database (if you want).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM