I have a gridview. I want to insert or update in table through datagridview....
rowleave event coding is given below-
Private Sub dataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Try If dataGridView1.CurrentRow.Cells(0).Value.ToString() IsNot Nothing Then For j As Integer = 0 To i - 1 s = "'" + dataGridView1.CurrentRow.Cells(j).Value & "'" & "," s1 = s1 + s Next s1 = s1.Substring(0, s1.Length - 1) Try If dataGridView1.Rows.Count > m Then sql = ("insert into" & " ") + tablenametext.Text & " " & "values" & "(" & s1 & ")" & ";" m += 1 Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() Else 'if (dataGridView1.CurrentRow.Cells[0].Value !=null) If t1 <> " " Then sql = (("update" & " ") + tablenametext.Text & " " & "set" & " " & t1 & "where" & " " & p & "=" & "'") + dataGridView1.CurrentRow.Cells(ind).Value & "'" & ";" Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() ' else 'MessageBox.Show("row is empty so fill values first"); End If End If Catch generatedExceptionName As Exception End Try s1 = "" t1 = "" End If Catch ee As Exception MessageBox.Show(ee.Message) End Try ' bind(); End Sub
and on cell state changed given below-
Private Sub dataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) For j As Integer = 0 To i - 1 t = dataGridView1.Columns(j).Name + "=" & "'" & dataGridView1.CurrentRow.Cells(j).Value.ToString() & "'" & "," t1 = t1 + t Next t1 = t1.Substring(0, t1.Length - 1) End Sub
There are two problems. Updating is not done through this and it gets null value for the last columns value of datagridview.....so plz try to resolve it asap....thanks....
rowleave event coding is given below-
Private Sub dataGridView1_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Try If dataGridView1.CurrentRow.Cells(0).Value.ToString() IsNot Nothing Then For j As Integer = 0 To i - 1 s = "'" + dataGridView1.CurrentRow.Cells(j).Value & "'" & "," s1 = s1 + s Next s1 = s1.Substring(0, s1.Length - 1) Try If dataGridView1.Rows.Count > m Then sql = ("insert into" & " ") + tablenametext.Text & " " & "values" & "(" & s1 & ")" & ";" m += 1 Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() Else 'if (dataGridView1.CurrentRow.Cells[0].Value !=null) If t1 <> " " Then sql = (("update" & " ") + tablenametext.Text & " " & "set" & " " & t1 & "where" & " " & p & "=" & "'") + dataGridView1.CurrentRow.Cells(ind).Value & "'" & ";" Dim cmd As New OdbcCommand(sql, c.cn) cmd.CommandText = sql cmd.ExecuteNonQuery() ' else 'MessageBox.Show("row is empty so fill values first"); End If End If Catch generatedExceptionName As Exception End Try s1 = "" t1 = "" End If Catch ee As Exception MessageBox.Show(ee.Message) End Try ' bind(); End Sub
and on cell state changed given below-
Private Sub dataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) For j As Integer = 0 To i - 1 t = dataGridView1.Columns(j).Name + "=" & "'" & dataGridView1.CurrentRow.Cells(j).Value.ToString() & "'" & "," t1 = t1 + t Next t1 = t1.Substring(0, t1.Length - 1) End Sub
There are two problems. Updating is not done through this and it gets null value for the last columns value of datagridview.....so plz try to resolve it asap....thanks....