Connecting Tech Pros Worldwide Help | Site Map

Datagridview

OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#1: Jul 16 '09
I have 2 datagridviews on my form.
DGview1 gets its rows from a .xls form
and
DGview2 gets its rows from the SQL database

Now i need to loop through DGview1 and remove the rows that are equal to the rows that are in DGview2

Expand|Select|Wrap|Line Numbers
  1. dim row as datagridviewrow
  2. for each row in datagridview1.rows
  3. next
could anyone help me out ?
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#2: Jul 17 '09

re: Datagridview


I have not worked much on Datagridviews. But first of all read all the filled rows in DGview1 and store it as, say, RDGView1. then read filled rows in DGview2 and store it in, say, RDGView2. Now use two For loops, first for RDGView1 & inner one for DGView2. After first loop statement store all column data in a variable like DDGView1 for each row one by one and in inner loop save data of all columns for row in , say, DDGView2. It may be like this

Expand|Select|Wrap|Line Numbers
  1. RDGView1=No. of filled rows in DGView1
  2. RDGView2=No. of filled rows in DGView2
  3. For i = 1 to RDGView1
  4.      DDGView1=data of first col of row i + data of second col of i row + ...+data of last col of row i (of DGView1)
  5.      For j = 1 TO RDGView2
  6.          DDGView2=data of first col of row i + data of second col of i row + ...+data of last col of row i (of DGView2)
  7.          IF DDGView2=DDGView1 then
  8.             here delete row j of DGView2
  9.          End If
  10.      Next
  11. Next
  12.  
I think I tried to make a logic to be used.
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 353
#3: Jul 17 '09

re: Datagridview


Expand|Select|Wrap|Line Numbers
  1. Try
  2.             'remove all the unsubscribed entries from the upload grid
  3.             Dim val3 As String = Nothing
  4.             Dim val4 As String = Nothing
  5.  
  6.             'loop through DG1
  7.             For Each row As DataGridViewRow In DataGridview1.Rows
  8.  
  9.                 'Check datagridview 1 and get Value
  10.                 If Not (row.IsNewRow) Then val3 = DataGridview1.Rows(row.Index).Cells(0).Value.ToString
  11.  
  12.                 'Loop through DG2
  13.                 For Each row1 As DataGridViewRow In DataGridView2.Rows
  14.  
  15.                     'Check DG2 and get Value
  16.                     If Not (row1.IsNewRow) Then val4 = DataGridView2.Rows(row1.Index).Cells(0).Value.ToString
  17.  
  18.                     'Compare values and remove from DG1
  19.                     If val3 = val4 Then
  20.                         ' Remove the duplicate from the upload grid
  21.                         If Not (row1.IsNewRow) Then DataGridview1.Rows.Remove(DataGridview1.Rows(row.Index))
  22.                     End If
  23.                 Next
  24.             Next
  25.             PleaseWait.Close()
  26.         Catch ex As Exception
  27.  
  28.         End Try
If i do it this way it only loops through DG1 once and removes the correct item and then its stops looping through
smartchap's Avatar
Familiar Sight
 
Join Date: Dec 2007
Location: Lucknow, India
Posts: 194
#4: Jul 18 '09

re: Datagridview


After deleting / removing a row total no. of rows in that grid will change, i.e. will be 1 less than previous. So take care of this and after removing a row recalculate total no. of rows in that grid. I think it will help u.
Reply