Prathap
Prathap

Reputation: 313

Change the datatable column value when Datagridview value is changed

I have two datagridview controls on a form.When we the selection changes in DGV1 the DGV2 rows gets filtered depending on ID in DGV1.Now if I make a change in the cell of DGV2 a columns called Amount gets updated in DGV1. I have achieved this using cell value changed and its working fine.For this I am using a datatable.So when i want to save changes in the dgv2 in the database.The value entered in DGV2 is not saved.For that I am also changing the value in datable depending on the row index.Below is the data in DGV1


ID Qty UnitPrice Amount
-----------------------

1    20   2        40
2    40   3        120



in DGV2 below is the data

ID  SellQty 
-----------
1    5   
2    10  
1    15
2    10

So when user click on any cell in DGV1 the DGV2 rows get filtered(as you can see in DGV1 for ID=1 there are 2 values).All this is fine. Below is the line of code i have written in Cellvalue changed of DGV2 so that the datatable cells are also updated as I need to save in the database using foreach loop.

dtBatch.Rows[i].SetField("SellQty", gvBatchDetails.Rows[i].Cells["SellQty"].Value);

I am using a variable i (int).But when the row is filtered when the user enters ID 2 in DGV1 the i value also changes to 0 for first row.Where as in Datatable ID 2 is at index 1.So how to make changes in the datatable in this case.

Upvotes: 0

Views: 1318

Answers (1)

Prathap
Prathap

Reputation: 313

I have solved the issue by getting the selected row from datagridview.

    DataRow[] rowList = QuoteSalesItemSearch.dtBatch.Select("BatchItemID='" + ItemID + "'" + "and BatchNo='" + BatchNo + "'");
  foreach (DataRow dr in rowList)
   dr["SellQty"] = gvBatchDetails.Rows[e.RowIndex].Cells["SellQty"].Value;

Upvotes: 0

Related Questions