0
votes

I have two forms Form1(DGV1) and Form2(DGV2) with Datagridview control on both the forms.

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

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.

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.

Can you please suggest a way to solve this.

Thank you.

1
Just to clarify: objQM.gvItemDetails is DGV1 and gvInventory is DGV2? At once you can add only one row (the selected one ("In Form2 Datagridview there is a checkbox in the first column and the user can select only one row at a time.")? Is this all code that is handling row adding?gzaxx
Yeah you are right .. gvItemDetails is the parent grid and gvInventory is the child grid.First I am checking whether the checkbox is checked in the gvInventory grid. if (Convert.ToBoolean(gvInventory.Rows[RowIndex].Cells[0].FormattedValue)) {Prathap

1 Answers

0
votes

You are adding for each row selected in gvInventory two rows in parent grid (is that wanted way of working?), if not try this:

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.