更改Datagridview值时更改数据表列值 [英] Change the datatable column value when Datagridview value is changed

查看:63
本文介绍了更改Datagridview值时更改数据表列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在表单上有两个datagridview控件。当DGV1中的选择发生变化时,DGV2行会根据DGV1中的ID进行过滤。如果我在DGV2的单元格中进行了更改,在DGV1中更新了名为Amount的列。我已经实现了这个使用单元格值更改和它的工作正常。为此我使用datatable.So当我想保存数据库中的dgv2中的更改。在DGV2中输入的值不保存。为此我也在改变数据中的值取决于行索引。下面是DGV1中的数据



ID数量UnitPrice金额

------ -----------------



1 - 20 - 2 - 40

2 - 40 - 3 - 120






以下DGV2中的
是数据



ID SellQty

-----------

1 - 5

2 - 10

1 - 15

2 - 10





所以当用户单击DGV1中的任何单元格DGV2行被过滤(如您在DGV1中可以看到ID = 1有2个值)。所有这一切都没问题。下面是我在Cellvalue中编写的代码行更改为DGV2,以便数据表单元格也会更新,因为我需要使用foreach循环保存在数据库中。



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







我正在使用变量i(int)。但是当用户在DGV1中输入ID 2时过滤行时,第一行的i值也会变为0.在数据表ID 2中是在索引1.如何在这种情况下对数据表进行更改。即使我用e.RowIndex替换i仍然会更新数据表中的相同行,因为我在CellEnter上过滤数据。

解决方案

我已经解决了的问题获取所选行来自 datagridview。 

DataRow [] rowList = QuoteSalesItemSearch.dtBatch。选择 BatchItemID =' + ItemID + ' + 和BatchNo =' + BatchNo + ');
foreach(DataRow dr in rowList)
dr [ SellQty] = gvBatchDetails.Rows [e.RowIndex] .Cells [ SellQty]值;


Hi,

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.Even if i Replace i with e.RowIndex still the same rows in datatable gets updated as I am filtering the data on CellEnter.

解决方案

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;


这篇关于更改Datagridview值时更改数据表列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆