Avatar of PNRT
PNRT

asked on 

VB.Net Removing Rows from DataGridView

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
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
PNRT
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of PNRT
PNRT

ASKER

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.
How are you loading the DataGridView? From DB, a List Object or something else and are you using a BindingSource to feed the DataGridView?
Avatar of PNRT
PNRT

ASKER

No, just running a bunch of queries and adding the result to the datagridview with

frmMain.DGV1.Rows.Add(New String() {Value1, Value2})
Can you show/post all relevant code for creating and filling the DataGridView control please.
Avatar of PNRT
PNRT

ASKER

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,
Avatar of PNRT
PNRT

ASKER

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
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.
Avatar of PNRT
PNRT

ASKER

.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..
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo