Connecting Tech Pros Worldwide Help | Site Map

Fill DataGridView into another DatagridView

Newbie
 
Join Date: May 2008
Posts: 21
#1: Mar 21 '09
Please help me out!
Am using VB.NET 2008 and I have two DataGridviews namely DataGridview1 and DataGridView2.. Now, I have 4 columns which are check1, Name, Address and Age.
My headache now is, I want after a user checks/selects rows from DataGridView1, then those rows should be filled in DataGridView2 on a click of a button.
Is that really possible? Help me out gurus.

Thanks in advance.

ben.
MrMancunian's Avatar
Expert
 
Join Date: Jul 2008
Location: Utrecht, The Netherlands
Posts: 274
#2: Mar 21 '09

re: Fill DataGridView into another DatagridView


Sure it is possible. Create a Sub that checks the value of the check-field in the datagridview1. You can use a For-Next-loop to go though your datagridview. Something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim a as Integer
  2. For a = 0 to Datatgridview1.Rows.Count - 1
  3.   If Datagridview1.Rows(a).Cells("Check1").Checked = True then
  4.     Datagridview2.Rows.Add(Datagridview1.Rows(a).Cells("Check1").Value, Datagridview1.Rows(a).Cells("Name").Value, Datagridview1.Rows(a).Cells("Address").Value, Datagridview1.Rows(a).Cells("Age").Value)
  5.   End If
  6. Next
  7.  
I didn't test this, so there are probably some flaws, but the technique should be something like this :-)
Newbie
 
Join Date: May 2008
Posts: 21
#3: Mar 23 '09

re: Fill DataGridView into another DatagridView


Hi, the code u sent could be helpful, but this line was underlined and it pops up a tooltip that reads checked is not a member of Systems.Windows.DataGridView

If DataGridView1.Rows(a).Cells("Check1").Checked = True Then

if u can please fix that bug for me.

ben
Member
 
Join Date: Mar 2009
Location: Kathmandu
Posts: 42
#4: Mar 23 '09

re: Fill DataGridView into another DatagridView


DataGridView1.Rows(a).Cells("Check1").Value= True
Newbie
 
Join Date: May 2008
Posts: 21
#5: Mar 24 '09

re: Fill DataGridView into another DatagridView


Thanx and God bless u, it worked. Anytime i click the fill button, the data in DataGridView2 gets duplicated. How can I make it fill the grid once. I used the exit for, to exit the loop after filling the DataGridView2 once, but that also did not work. Any suggestions?
Ben
Member
 
Join Date: Mar 2009
Location: Kathmandu
Posts: 42
#6: Mar 24 '09

re: Fill DataGridView into another DatagridView


Just clear DataGridView2 before loop.

DataGridView2.Rows.Clear()
Newbie
 
Join Date: May 2008
Posts: 21
#7: Mar 25 '09

re: Fill DataGridView into another DatagridView


Thank you a lot!
It works. god bless you all.

Ben
Reply