Solved

VB.Net Removing Rows from DataGridView

Posted on 2016-09-05
9
33 Views
Last Modified: 2016-09-05
Hi Experts
I have a datagridview with two columns and about 250 rows    There are 20 rowsin the second column with the value "No Information"
and I want to delete these.  I have rigged a button just for testing.

This code for testing works but only gives me a MessageBox 9 times not 20, I thought it would give me 20

   
  Dim DGV1Row As DataGridViewRow
            Dim CheckValue As String

            For Each DGV1Row In DGV1.Rows
                CheckValue = DGV1Row.Cells(1).Value
                If CheckValue = "No Information" Then
                    MsgBox(CheckValue)
                End If
            Next

Open in new window


This Code is intermittent, sometimes removing 4 "No Information"  and sometimes 8, not 20

         
  Dim DGV1Row As DataGridViewRow
            Dim CheckValue As String
            For Each DGV1Row In DGV1.Rows
                CheckValue = DGV1Row.Cells(1).Value
                If CheckValue = "No Information" Then
                     DGV1.Rows.Remove(DGV1Row)
                End If
            Next

Open in new window


The data in the Rows is identical each time I run it and I even tried comparing the values length to see if they were not the same
I've also tried variations of For i  = 0 To DGV1.Rows.Count  With no success.

What am I doing wrong??

Many Thanks
0
Comment
Question by:PNRT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41785185
Hi  PNRT;

Try it like this and see if it gives you the results you are looking for.
Dim DGV1Row As DataGridViewRow
Dim CheckValue As String
'' Make the -2 to -1 if the DataGridView does not display a blank row at the end
For i = DGV1.Rows.Count - 2 To 0 Step -1
    CheckValue = DGV1.Rows.Item(i).Cells(1).Value
    If CheckValue = "No Information" Then
        DGV1.Rows.RemoveAt(i)
    End If
Next

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 41785196
Hi Fernando
Many Thanks for the reply

No luck I'm afraid
I added  Dim i As Integer

It does the same, deletes four or five of the values only.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41785202
How are you loading the DataGridView? From DB, a List Object or something else and are you using a BindingSource to feed the DataGridView?
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 2

Author Comment

by:PNRT
ID: 41785209
No, just running a bunch of queries and adding the result to the datagridview with

frmMain.DGV1.Rows.Add(New String() {Value1, Value2})
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41785223
Can you show/post all relevant code for creating and filling the DataGridView control please.
0
 
LVL 2

Author Comment

by:PNRT
ID: 41785227
Thank you Fernando
I will step back a bit and remove everything except the datagridview and the added data code and see if I can reproduce it.  Thanks again,
0
 
LVL 2

Author Comment

by:PNRT
ID: 41785260
Hi
I think I have found the problem and the fix although I don't know why VB seems to have added rubbish to some, not all of my queries and produced this

frmMain.DGV1.Rows.Add(New String() {Value1, CType(CType(CType(CType(CType(CType(CType(Value2, String), String), String), String), String), String), String)})

I don't understand why but it looks as though for some queries I was not testing again a string at all.

By  just Adding .......    Value2.ToString       removes it from the Datagridview
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41785265
How are you doing your query? What technology are you using ADO.Net, Entity Framework, Linq to SQL or something else? It is hard to understand what is happening without knowing this.
0
 
LVL 2

Author Comment

by:PNRT
ID: 41785270
.Net 4.5.2,     querying win32 class and  securityCenter2 and other classes,   but I have installed CodeRush recently and not really sure what I'm doing with yet.  Maybe I have it set up wrong.
Thanks for your help.  At least I could see where I was going wrong and it wasn't in the first code..
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question