Click here to Skip to main content
11,479,170 members (64,036 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGridView
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 6-Feb-13 23:33pm
Comments
CHill60 at 7-Feb-13 6:33am
   
how are you "showing" form1 ... is your Done button creating a new instance everytime?
Prathap Gangireddy at 7-Feb-13 14:25pm
   
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());
Prathap Gangireddy at 7-Feb-13 14:29pm
   
Can anyone suggest a workaround to validate if the same row is added into DGV1 on Form1 when AddItems button is clicked on Form2.I mean no duplicate rows must be added.
Prathap Gangireddy at 8-Feb-13 2:06am
   
<pre lang="xml">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.</pre>

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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) >
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 215
1 F-ES Sitecore 195
2 OriginalGriff 185
3 Frankie-C 120
4 DamithSL 85
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,366
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100