简体   繁体   English

将Datagridview行从一个网格添加到另一个网格

[英]Adding Datagridview rows from one grid to another

I have two forms Form1(DGV1) and Form2(DGV2) with Datagridview control on both the forms. 我有两个窗体Form1(DGV1)和Form2(DGV2),两个窗体上都有Datagridview控件。

On Form1 there is a button link to Form2 where user can add rows to Form1 datagridview(DGV1) ..In Form2 Datagridview there is a checkbox in the first column and the user can select only one row at a time.So there are two button on Form2 1) Done 2) Add Items 在Form1上有一个到Form2的按钮链接,用户可以在其中向Form1 datagridview(DGV1)添加行。在Form2 Datagridview中,第一列中有一个复选框,用户一次只能选择一行,因此有两个按钮在Form2上1)完成2)添加项目

When user clicks on Add Items by selecting the rows one after another the rows must be added to the datagridview on Form1 and when Done button is clicked Form 1 should be displayed with all the rows that are added. 当用户通过依次选择行来单击“添加项目”时,必须将行添加到Form1上的datagridview中,并且当单击“完成”按钮时,应该显示包含所有行的表单1。

int index = objQM.gvItemDetails.Rows.Add();
int Sno = index + 1;
string Desc = gvInventory.Rows[RowIndex].Cells[6].Value.ToString();
int Sellqty = LoadSellQty();
 decimal UnitCost = Convert.ToDecimal(gvInventory.Rows[RowIndex].Cells[8].Value.ToString());
 decimal Amount = LoadSellQty() * UnitCost;
 objQM.gvItemDetails.Rows.Insert(index,false, Sno, Desc, Sellqty, UnitCost, Amount);

I am able to add one row into Form1 datagridview but when the second row is added the row which was added earlier is lost. 我可以向Form1 datagridview中添加一行,但是当添加第二行时,先前添加的行会丢失。

Can you please suggest a way to solve this. 您能否提出解决此问题的方法?

Thank you. 谢谢。

You are adding for each row selected in gvInventory two rows in parent grid (is that wanted way of working?), if not try this: 您要为在gvInventory选择的每一行添加父网格中的两行(这是想要的工作方式吗?),如果不尝试的话:

int index = objQM.gvItemDetails.Rows.Add();
DataGridViewRow row = objQM.gvItemDetails.Rows[index]; //here you get a reference to added row
row.Cells[0] = false;
row.Cells[1] = index + 1; //old Sno
row.Cells[2] = gvInventory.Rows[RowIndex].Cells[6].Value.ToString();
row.Cells[3] = LoadSellQty();
row.Cells[4] = Convert.ToDecimal(gvInventory.Rows[RowIndex].Cells[8].Value.ToString());
row.Cells[5] = LoadSellQty() * UnitCost;

This way you add new row and fill it with values. 这样,您可以添加新行并用值填充它。 From code you gave it is not clear why first row is lost, so maybe I will need some more information. 从您提供的代码尚不清楚为什么第一行会丢失,因此也许我将需要更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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