DataGridView单元单击(数据具有零值。无法在空值上使用CadatagridView CellClickll方法或属性。)
我将文本框连接到单击单元单元的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)