Home > Net >  DataGridView Cell Click( The data has a null value. Cannot caDataGridView CellClickll method or prop
DataGridView Cell Click( The data has a null value. Cannot caDataGridView CellClickll method or prop

Time:06-24

I connected the textboxes to the datagridview on cell click, in order to modify from the textboxes, a column (8) is empty, even if in the SQL Server table I set it to allow null value, in the visual studio when I click it gives me an error( The data has a null value. Cannot caDataGridView CellClickll method or property on null values.), here is my code

 Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Try
        If connection.State = ConnectionState.Open Then
            connection.Close()
        End If
        connection.Open()
        i = Convert.ToInt32(DataGridView1.SelectedCells.Item(0).Value.ToString())
        cmd = connection.CreateCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Select * From StoricoLotto WHERE ID=" & i & ""
        cmd.ExecuteNonQuery()
        Dim table As New DataTable()
        Dim adapter As New SqlDataAdapter(cmd)
        adapter.Fill(table)
        Dim dr As SqlClient.SqlDataReader
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        While dr.Read
            txtId.Text = dr.GetValue(0).ToString()
            dtpDataEstrazione.Text = dr.GetDateTime(1).ToString()
            txtRuota.Text = dr.GetString(2).ToString()
            txtEstratto1.Text = dr.GetValue(3).ToString()
            txtEstratto2.Text = dr.GetValue(4).ToString()
            txtEstratto3.Text = dr.GetValue(5).ToString()
            txtEstratto4.Text = dr.GetValue(6).ToString()
            txtEstratto5.Text = dr.GetValue(7).ToString()
            txtRicercaEstrazioni.Text = dr.GetString(8).ToString()
        End While
        connection.Close()
    Catch ex As Exception
        MsgBox(ex.Message) 'show error msg'
    End Try
End Sub
 Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)
        txtId.Text = row.Cells("id").Value.ToString
        dtpDataEstrazione.Text = row.Cells("Data_Estraz").Value.ToString
        txtRuota.Text = row.Cells("Ruota").Value.ToString
        txtEstratto1.Text = row.Cells("Estratto1").Value.ToString
        txtEstratto2.Text = row.Cells("Estratto2").Value.ToString
        txtEstratto3.Text = row.Cells("Estratto2").Value.ToString
        txtEstratto4.Text = row.Cells("Estratto3").Value.ToString
        txtEstratto5.Text = row.Cells("Estratto4").Value.ToString
        txtEstratto1.Text = row.Cells("Estratto5").Value.ToString
        txtRicercaEstrazioni.Text = row.Cells("RicercaEstrazioni").Value.ToString
    End If
End Sub

CodePudding user response:

 Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)
        txtId.Text = row.Cells("id").Value.ToString
        dtpDataEstrazione.Text = row.Cells("Data_Estraz").Value.ToString
        txtRuota.Text = row.Cells("Ruota").Value.ToString
        txtEstratto1.Text = row.Cells("Estratto1").Value.ToString
        txtEstratto2.Text = row.Cells("Estratto2").Value.ToString
        txtEstratto3.Text = row.Cells("Estratto2").Value.ToString
        txtEstratto4.Text = row.Cells("Estratto3").Value.ToString
        txtEstratto5.Text = row.Cells("Estratto4").Value.ToString
        txtEstratto1.Text = row.Cells("Estratto5").Value.ToString
        txtRicercaEstrazioni.Text = row.Cells("RicercaEstrazioni").Value.ToString
    End If
End Sub
  • Related