473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Loop

OuTCasT
374 256MB
I have 2 gridviews.
i need to loop through DG1 and DG2 and if there is a value present in DG2 i would like to remove it out of DG1.

Expand|Select|Wrap|Line Numbers
  1. Try
  2.             For rowIndex = 0 To DataGridview1.RowCount - 2
  3.  
  4.                 email = DataGridview1.Rows(rowIndex).Cells(0).Value.ToString
  5.  
  6.                 For rowIndex1 = 0 To DataGridView2.RowCount - 2
  7.  
  8.                     email2 = DataGridView2.Rows(rowIndex1).Cells(0).Value.ToString
  9.  
  10.                     If email = email2 Then
  11.                         DataGridview1.Rows.Remove(DataGridview1.Rows(rowIndex))
  12.                     End If
  13.  
  14.                 Next
  15.  
  16.             Next
  17.  
  18.  
  19.  
  20.         Catch ex As Exception
  21.  
  22.         End Try
This is the code that i am using but it doesnt seem to work properly.

I know that the rowIndex changes when something is deleted from DG1 but i cant seem to make the code work for me.
Jul 21 '09 #1
6 1411
Plater
7,872 Expert 4TB
If DG1 is your outer loop, everytime you delete an entry from it, you must subtract one from the index for DG1.
Would it not work better to make DG2 your outer loop?(Unless DG2 is much bigger then DG1)
Jul 21 '09 #2
OuTCasT
374 256MB
DG1 is the large one.
I see that it starts on rowIndex 1 usually after u have deleted a row, depending where the row was in the index it wld most probably change.

Now when ive deleted the first duplicated row i try set the rowindex to start at 0 again but then the loop becomes infinate.
Jul 21 '09 #3
Plater
7,872 Expert 4TB
You don't want to set it to 0, just subtract one.
So thenyou would have this:
Expand|Select|Wrap|Line Numbers
  1. If email = email2 Then 
  2.    DataGridview1.Rows.Remove(DataGridview1.Rows(rowIndex)) 
  3.    rowIndex=rowIndex-1
  4. End If 
  5.  
Jul 21 '09 #4
OuTCasT
374 256MB
Thanks that seems to have done the trick although it does come up with an error. But i have put it in a try catch so that it skips the error

Expand|Select|Wrap|Line Numbers
  1. Try
  2.             For rowIndex = 0 To DataGridview1.RowCount - 2
  3.  
  4.                 email = DataGridview1.Rows(rowIndex).Cells(0).Value.ToString
  5.  
  6.                 For rowIndex1 = 0 To DataGridView2.RowCount - 2
  7.  
  8.                     email2 = DataGridView2.Rows(rowIndex1).Cells(0).Value.ToString
  9.  
  10.                     If email = email2 Then
  11.                         DataGridview1.Rows.Remove(DataGridview1.Rows(rowIndex))
  12.                         rowIndex = rowIndex - 1
  13.                     End If
  14.  
  15.                 Next
  16.  
  17.             Next
  18.  
  19.  
  20.  
  21.         Catch ex As Exception
  22.  
  23.         End Try
Jul 22 '09 #5
Plater
7,872 Expert 4TB
Hmm what is the exception that it throws? Is it IndexOutOfRange ? I am not sure how VB does its looping, but if you remove the first object (when the index=0) then the removal would make the index be -1, which might cause problems?
Jul 22 '09 #6
OuTCasT
374 256MB
Its either object not set to a reference or index out of range.
What i did was i fill the DG1 from the excel sheet. Then build a query string containing the email from DG1 and query the database for those email addresses. Then fill DG2 with those email addresses. I did a rowcount on the DG2 and used the loop in question above and put it in a loop

Expand|Select|Wrap|Line Numbers
  1. dim i as integer
  2. for i = 0 to label1.text
  3.      RemoveLists
  4. next
so it would definately loop through the amounts found in DG2 and remove them from DG1.
Jul 23 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.