Link to home
Start Free TrialLog in
Avatar of DeniseGoodheart
DeniseGoodheart

asked on

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
SOLUTION
Avatar of gnar
gnar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DeniseGoodheart
DeniseGoodheart

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ?
Hello Jpaulino:

That is alright because you experts have a difficult job trying to help everyone.
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
>> 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.
Hello jpaulino:

Thanks for your great suggestion.

Cheers,
Denise
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

https://www.experts-exchange.com/questions/23143545/Identify-the-Previous-Row-After-the-User-Executes-the-RowEnter-Event-Using-DataGridView.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
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
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
Hello Roger:

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

Cheers,
Denise