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

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
DeniseGoodheartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gnarCommented:
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
Jorge PaulinoIT Pro/DeveloperCommented:
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
DeniseGoodheartAuthor Commented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

SanclerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jorge PaulinoIT Pro/DeveloperCommented:
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
DeniseGoodheartAuthor Commented:
Hello Jpaulino:

That is alright because you experts have a difficult job trying to help everyone.
0
DeniseGoodheartAuthor Commented:
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
Jorge PaulinoIT Pro/DeveloperCommented:
>> 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
DeniseGoodheartAuthor Commented:
Hello jpaulino:

Thanks for your great suggestion.

Cheers,
Denise
0
SanclerCommented:
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
DeniseGoodheartAuthor Commented:
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
SanclerCommented:
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
DeniseGoodheartAuthor Commented:
Hello Roger:

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

Cheers,
Denise
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.