Get a DataGridView Column Value

Posted on 2008-02-12
Medium Priority
Last Modified: 2013-12-17
Good Day:

I am using VB.NET 2005 with the DataGridView control called grdSODetail.  After the user enters a value in the Cust P/N grdSODetail column; I would like to retrieve the Cust P/N value using one of the DataGridViewCell events. The following code does not work because the CellValidating event is not able to set the Cust P/N column value in time to retrieve the value and returns the following error:
Null Reference; Object Reference Not Set to an Instance of an Object.

My code is as follows:
Private Sub grdSODetail_CellValidating(ByVal sender As Object, _
                ByVal e As DataGridViewCellValidatingEventArgs) _
                Handles grdSODetail.CellValidating

If String.IsNullOrEmpty(e.FormattedValue.ToString()) = False Then
            sCustPN = grdSODetail.Rows(iRow).Cells("Cust P/N").Value.ToString
                    End If
    End Sub

Can anyone suggest a DataGridView event I can use to run my code and retrieve the value of the Cust P/N column value?  Or perhaps something else to make this work?

Thank You,
Question by:DeniseGoodheart
  • 3
LVL 48

Accepted Solution

Jorge Paulino earned 2000 total points
ID: 20879990
Hi DeniseGoodheart,

You can use the cell endedit:

Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        MsgBox(DataGridView1(e.ColumnIndex, e.RowIndex).Value)
End Sub

LVL 48

Assisted Solution

by:Jorge Paulino
Jorge Paulino earned 2000 total points
ID: 20880125
you can validate this way

        If DataGridView1(e.ColumnIndex, e.RowIndex).Value = "" Then
            MsgBox("Not valid!")
            MsgBox(DataGridView1(e.ColumnIndex, e.RowIndex).Value)
        End If

Author Comment

ID: 20880393
Many thanks JPaulino.

LVL 48

Expert Comment

by:Jorge Paulino
ID: 20880440
Glad I could help Denise

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

592 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