Shreekant
Shreekant

Reputation: 469

Getting Current Cell value from DataGridView

I am trying to get current cell value of a DataGridView. This current cell value is the product name. As the user types the product name I want to populate the Unit on its own. So I need to get the current cell value so that I can pass it to database and get the expected unit for the product. I used a variable (Product) to store the current cell value and whenever I debug the code this variable contains nothing.

I am Putting my code below for you to Get an idea

Private Sub dataGridView2_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView2.CellValidating
    If e.ColumnIndex = 1 Then

        Dim product As String = DataGridView2.CurrentCell.Value
        Dim rowind As Integer = DataGridView2.CurrentCell.RowIndex
        Dim columnind As Integer = DataGridView2.CurrentCell.ColumnIndex
        Using ConnectionString As New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;")
            Dim strSQL As String = ("SELECT Unit from b_productMst where productname =" + product)
            Dim SQLcommand As New SqlCommand(strSQL, ConnectionString)
            ConnectionString.Open()
            Dim reader As SqlDataReader
            reader = SQLcommand.ExecuteReader()
            DataGridView2.Rows(rowind).Cells(columnind) = reader.Item(0)
        End Using
        End If
    End Sub

I even tried using

 product = DataGridView2.Rows(rowind).Cells(columnind).Value

but of no use.

I have mentioned my goal above. Please let me know if there is any other way to achieve this.

Upvotes: 1

Views: 165

Answers (1)

Mary
Mary

Reputation: 15091

Don't use the name of the class as the name of your variable. As in, don't Dim SqlCommand As New SqlCommand.

The command and the reader both need using blocks. (They expose .Dispose methods). The command is included in the same block as the connection. Note the comma at the end of the first Using line.

Always use parameters to avoid Sql Injection. I had to guess at the datatype and field size. Check you database for the correct values and adjust the code accordingly.

The DataReader starts before the first record. You need to call .Read to move to the first record.

Private Sub dataGridView1_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If e.ColumnIndex = 1 Then
        Dim product As String = DataGridView1.CurrentCell.Value.ToString 'Value is an Object
        Dim rowind As Integer = DataGridView1.CurrentCell.RowIndex
        Dim columnind As Integer = 1 'The If block code wouldn't be executing unless the ColumnIndex = 1
        Using Con As New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;"),
            cmd As New SqlCommand("SELECT Unit from b_productMst where productname = @Product;", Con)
            cmd.Parameters.Add("@Product", SqlDbType.VarChar, 100).Value = product
            Con.Open()
            Using reader = cmd.ExecuteReader()
                reader.Read()
                DataGridView1.Rows(rowind).Cells(columnind + 1).Value = reader.Item(0).ToString
            End Using
        End Using
    End If
End Sub

Unless the the b_productMst table is huge, I would far prefer to see all the data downloaded in the Form.Load and stored in memory as a DataTable or Dictionary. This would avoid repeated hits on the database.

Private dt As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Using Con As New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;"),
            cmd As New SqlCommand("SELECT productname, Unit from b_productMst; ", Con)
        Con.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
End Sub

Private Sub dataGridView1_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If e.ColumnIndex = 1 Then
        Dim product As String = DataGridView1.CurrentCell.Value.ToString 'Value is an Object
        Dim rowind As Integer = DataGridView1.CurrentCell.RowIndex
        Dim columnind As Integer = 1 'The If block code wouldn't be executing unless the ColumnIndex = 1
        DataGridView1.Rows(rowind).Cells(columnind + 1).Value = dt.Select("productname = product")(0)(2)
    End If
End Sub

Upvotes: 1

Related Questions