Adding Datagridview rows from one grid to another

Posted by Nkkppp under C# on 2/7/2013 | Points: 10 | Views : 3607 | Status : [Member] | Replies : 4
Hi All,

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.


Posted by: Pavanandey on: 2/7/2013 [Member] Bronze | Points: 25

can you expain in clear... is that a win app or web app

Pavan Kumar
Mark Answer if this fits the need

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nkkppp on: 2/7/2013 [Member] Starter | Points: 25

Hi Pavan,

I have mentioned Datagridview so its obvious dat it is a winapp.Anywaz I have resolved the issue.Below is my code.

int index= objQM.gvItemDetails.Rows.Add(); 

DataGridViewRow row = (DataGridViewRow)objQM.gvItemDetails.Rows[index];
decimal UnitCost = Convert.ToDecimal(gvInventory.Rows[RowIndex].Cells[8].Value.ToString());
row[0] = false; row[1] = 1;
row[2] = gvInventory.Rows[RowIndex].Cells[6].Value.ToString();
row[3] = LoadSellQty();
row[4] = Convert.ToDecimal(gvInventory.Rows[RowIndex].Cells[8].Value.ToString());
row[5] = LoadSellQty() * UnitCost;
row[7] = Convert.ToInt32(gvInventory.Rows[RowIndex].Cells[1].Value.ToString())

Can anyone suggest a workaround to validate if the same row is added into DGV1 (in Form1) when AddItems button is clicked on Form2.I mean no duplicate rows must be added

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nkkppp on: 2/8/2013 [Member] Starter | Points: 25

Hi ,

I have solved the problem.

ItemID is a unique column in the grid so used that to check whether an additional row is added.
objQM.gvItemDetails.Rows.Cast<DataGridViewRow>().Count(c => c.Cells[7].EditedFormattedValue.ToString() == ItemID) > 0)

one more problem is..Suppose I have added a row to the Form1 datagridview from Form2 by clicking AddItems button ..then Done button.Suppose I wanted to add another row by clicking on the link button in Form1 which takes me to Form2..Now the gridview gets empty and a completely new row is added.So i want to know how if ROW1 is added to Form1 DGV and clicked on DONE button...and again if I want to add another row ROW2.Both Row1 and Row2 shud exist but that is not happening.How to achieve this.

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rimi1289 on: 2/9/2013 [Member] Starter | Points: 25

Found this link and I am sure it will be helpful for you.

Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response