繁体   English   中英

DataGridView 单元格单击(数据具有空值。不能在空值上使用 caDataGridView CellClickll 方法或属性。)

[英]DataGridView Cell Click( The data has a null value. Cannot caDataGridView CellClickll method or property on null values.)

我在单元格单击时将文本框连接到 datagridview,为了从文本框进行修改,列 (8) 为空,即使在 SQL Server 表中我将其设置为允许空值,在 Visual Studio 中单击它时也是如此给我一个错误(数据有一个空值。不能在空值上使用 caDataGridView CellClickll 方法或属性。),这是我的代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM