DataGridView单元单击(数据具有零值。无法在空值上使用CadatagridView CellClickll方法或属性。)

发布于 2025-02-07 15:42:03 字数 2556 浏览 5 评论 0原文

我将文本框连接到单击单元单元的DataGridView,以从文本框中修改,即使在SQL Server表中,列(8)是空的,即使在SQL Server表中,我将其设置为允许NULL值,当我单击它时在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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2025-02-14 15:42:03
 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文