?
Solved

Error: System.Windows.Forms.DataGridViewRow Not Collection Type.

Posted on 2008-02-08
13
Medium Priority
?
465 Views
Last Modified: 2013-12-17
Good Day:

I am using VB.NET 2005 and the Microsoft DataGridView.  I need to iterate though the DataGridViewRow previous row and do cell validation.  I have identified the previous row by using the following:
        Dim i As Int32
        i = e.RowIndex-1

My problem is that the following code gives me an error that reads Expression is of type System.Windows.Forms.DataGridViewRow, which is not a collection type:
        For Each row As DataGridViewRow In DataGridView1.Rows(i)

My entire code is as follows:
Private Sub DataGridView1_Enter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter

        Dim i As Int32
        i = e.RowIndex-1
        For Each row As DataGridViewRow In DataGridView1.Rows(i)
            If IsDBNull(row.Cells("Artist").Value) Then
                MsgBox("No Data")
            End If
        Next
    End Sub

Any suggestions on how to fix this?
Thanks,
Denise
0
Comment
Question by:DeniseGoodheart
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 2

Assisted Solution

by:gnar
gnar earned 100 total points
ID: 20851973
I believe "DataGridView1.Rows(i)" should be returning a single row, but you're trying to iterate through it as though it was a collection of rows.
0
 
LVL 48

Assisted Solution

by:Jorge Paulino
Jorge Paulino earned 100 total points
ID: 20852072
Try to change it to this:

For Each row As DataGridViewRow In DataGridView1.Rows
     If IsDBNull(row.Cells("Artist").Value) Then
           MsgBox("No Data")
    End If
Next
0
 

Author Comment

by:DeniseGoodheart
ID: 20852099
jpaulino:

Thanks for your suggestion, but it will not work because I need to iterate the previous row and not the current row.  This would work only if I could set focus to the previous row first and then run the code.

Cheers,
Denise
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 34

Accepted Solution

by:
Sancler earned 1800 total points
ID: 20852182
Why not just


        Dim i As Int32
        i = e.RowIndex-1
        If IsDBNull(DataGridView1.Rows(i).Cells("Artist").Value) Then
            MsgBox("No Data")
        End If

But how do you know, just because a row has been entered, that the previous row was the one with an index of 1 less.  What happens if the user was in row 1 and then clicks in row 3?

Roger
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20852190
Sorry I don't understand it well.

>> I need to iterate though the DataGridViewRow previous row and do cell validation
Can you be more clear and give an example ?
0
 

Author Comment

by:DeniseGoodheart
ID: 20852329
Hello Jpaulino:

That is alright because you experts have a difficult job trying to help everyone.
0
 

Author Comment

by:DeniseGoodheart
ID: 20852350
Hello Sancler:

Your suggestion worked great; many thanks.  You raise a very good point.  I suppose I could interate through the entire grid and do data validation again before I do a batch insert.  What do you think?

Thank You,
Denise
0
 
LVL 48

Expert Comment

by:Jorge Paulino
ID: 20852442
>> But how do you know, just because a row has been entered, that the previous row was the one with an index of 1 less.  What happens if the user was in row 1 and then clicks in row 3?

Whell you can use a private variable to indicate the entered row. Each time you enter a row you assing the current position to the variable and then work with that.
0
 

Author Comment

by:DeniseGoodheart
ID: 20852523
Hello jpaulino:

Thanks for your great suggestion.

Cheers,
Denise
0
 
LVL 34

Expert Comment

by:Sancler
ID: 20852541
What is the best way of validating depends on the context.  But if you have done it once - properly ;-) - there should be no need to go through everything again to revalidate.

There are basically three models: do it when the user leaves the cell, or when the user leaves the row/record, or when the user (or the system) wants to commit the changes to the database.

What I'm not quite with at the moment - and this was the burden of my answer in your question

http://www.experts-exchange.com/Microsoft/Development/.NET/Visual_Studio_.NET_2005/Q_23143545.html

- is why you are thinking about validation (of a "previous" item) when a user ENTERs a new item (whether a cell or a row/record) rather than when s/he LEAVEs it (and it is still, for this purpose, the "current" item).

If you can explain that, I might be better able to form a view on your later question.

Roger
0
 

Author Comment

by:DeniseGoodheart
ID: 20852723
Hello Roger:

This is all new to me, so I am learning as I go along.  You recommend that I use the Cell Leave event?  Actually that would probably work better because my production grid will have a lot more cells to enter data.  And in my production grid (currently I'm using a test grid) if certain cells have data then other cells must have data as well.  I hope I am making sense.  And then I need to know how to do a focus on the cell so that the user knows which cell needs data entry.

Thank You,
Denise

Thank You,
Denise
0
 
LVL 34

Expert Comment

by:Sancler
ID: 20853129
Denise

I'm now tied up for a few hours.  But, briefly, just so as not to hold you up, can I suggest that you do a bit of reading up - in the help files or on the MS VB site - about Validation.  In particular, there's a How To article on datagridview validation.  I think it would also be worth you looking at the DataGridView's .CellValidating and .RowValidating events.  This is not to suggest that you cannot "roll your own" with - for example - .CellLeave, but before encouraging you down that road, I think it might be worth you dipping into the "built-in" validation aids that the DGV provides.

I'll look in again later.  If you've any specific issues arisign from this comment, I'll try and deal with them then.

Roger
0
 

Author Comment

by:DeniseGoodheart
ID: 20854873
Hello Roger:

Thanks for all your help.  I am sure I will be posting more questions.

Cheers,
Denise
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

594 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