Updating a DataTable column row using values for a primary key?

B

BStorm

Does anyone know the best way to update a DataTable row column using
specific values for a multipart primary key?

For example, updating an OrderAmt column in an OrderDetail table where the
primary key consists of an OrderNo and DetailSeqNo?
 
M

Mohamoss

Hi
The best way would be, to get a reference to the row then edit it.
If you define a key to the table then you can use the table.find method to
get the data columns. You pass to this function the value of the key that
you want to match once you get the value you can do the editing.
For example in your case , lets say you are looking for a row where OrderNo
2 and DetailSeqNo 3

DataRow to_be_edited = mytable.Rows.Find(2,3);
Then you do the editing on to_be_edited
To_be_edited["OrderAmt"] = "new value";


But note you have to define the primary key in the DataTable Object it is
not enough that it is define in the source database.
You define the primary key of the table by passing an array of a
DataColumns to the primary key property of the data table.
You do it like so

Mytable.PrimaryKey = new DataColumn[] { OrderNo , DetailSeqNo} // assuming
you defined the //data columns OrderNo and DetailSeqNo that point to
these columns inside the table
hope this explains it
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top