adwooley2
asked on
Datagridview - updating problems part II
Sancler, I hope you're out there somewhere....
I have a datagrid that's bound directly to a dataset. I also have a button on my form with following eventhandler:
Private Sub btnUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
DataGridView1.BindingConte xt(ds.Tabl es(0)).End CurrentEdi t()
Try
For Each row As DataRow In ds.Tables(0).Rows
If row.RowState = DataRowState.Modified Then
db.CropMasterUpdate(IIf(Is DBNull(row .Item(0)), "", row.Item(0)), _
IIf(IsDBNull(row.Item(1)), "", row.Item(1)), _
IIf(IsDBNull(row.Item(2)), "", row.Item(2)), _
IIf(IsDBNull(row.Item(3)), "", row.Item(3)), _
IIf(IsDBNull(row.Item(4)), "", row.Item(4)), _
IIf(IsDBNull(row.Item(5)), 0, row.Item(5)), _
IIf(IsDBNull(row.Item(6)), 0, row.Item(6)), _
IIf(IsDBNull(row.Item(7)), 0, row.Item(7)), _
IIf(IsDBNull(row.Item(8)), 0, row.Item(8)))
End If
BindGrid()
Next
Catch ex As Exception
End Try
End Sub
I also have textboxes and a populate item which goes through selected rows and fills in the cells:
Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
Me.Cursor = Cursors.WaitCursor
If Me.DataGridView1.SelectedR ows.Count > 0 Then
For Each row As DataGridViewRow In Me.DataGridView1.SelectedR ows
row.Cells(5).Value = IIf(Me.txtTop.Text = "", 0, Me.txtTop.Text))
row.Cells(6).Value = IIf(Me.txtBottom.Text = "", 0, Me.txtBottom.Text)
row.Cells(7).Value = IIf(Me.txtLeft.Text = "", 0, Me.txtLeft.Text)
row.Cells(8).Value = IIf(Me.txtRight.Text = "", 0, Me.txtRight.Text)
ds.AcceptChanges()
Next
End If
Me.Cursor = Cursors.Default
End Sub
However, when I make a change on a single row and hit update, it doesn't pick up the current row as having been editted (and therefore doesn't update the database). The row.RowState shows "unchanged". Sometimes when I make a bulk update to the datagridview, it doesn't even pick up those changes for reasons I do not know.
Also, when I make a change directly in the Datagridview, it sometimes picks up the change and saves... sometimes doesn't. Is there a sure-fire way to pick up all the changes to the datagridview so that I can save to the database?
This is rather urgent. All replies are appreciated.
Regards,
I have a datagrid that's bound directly to a dataset. I also have a button on my form with following eventhandler:
Private Sub btnUpdate_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
DataGridView1.BindingConte
Try
For Each row As DataRow In ds.Tables(0).Rows
If row.RowState = DataRowState.Modified Then
db.CropMasterUpdate(IIf(Is
IIf(IsDBNull(row.Item(1)),
IIf(IsDBNull(row.Item(2)),
IIf(IsDBNull(row.Item(3)),
IIf(IsDBNull(row.Item(4)),
IIf(IsDBNull(row.Item(5)),
IIf(IsDBNull(row.Item(6)),
IIf(IsDBNull(row.Item(7)),
IIf(IsDBNull(row.Item(8)),
End If
BindGrid()
Next
Catch ex As Exception
End Try
End Sub
I also have textboxes and a populate item which goes through selected rows and fills in the cells:
Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
Me.Cursor = Cursors.WaitCursor
If Me.DataGridView1.SelectedR
For Each row As DataGridViewRow In Me.DataGridView1.SelectedR
row.Cells(5).Value = IIf(Me.txtTop.Text = "", 0, Me.txtTop.Text))
row.Cells(6).Value = IIf(Me.txtBottom.Text = "", 0, Me.txtBottom.Text)
row.Cells(7).Value = IIf(Me.txtLeft.Text = "", 0, Me.txtLeft.Text)
row.Cells(8).Value = IIf(Me.txtRight.Text = "", 0, Me.txtRight.Text)
ds.AcceptChanges()
Next
End If
Me.Cursor = Cursors.Default
End Sub
However, when I make a change on a single row and hit update, it doesn't pick up the current row as having been editted (and therefore doesn't update the database). The row.RowState shows "unchanged". Sometimes when I make a bulk update to the datagridview, it doesn't even pick up those changes for reasons I do not know.
Also, when I make a change directly in the Datagridview, it sometimes picks up the change and saves... sometimes doesn't. Is there a sure-fire way to pick up all the changes to the datagridview so that I can save to the database?
This is rather urgent. All replies are appreciated.
Regards,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
5) you could set the column's default value in the datatable to "0" instead of checking for nulls using the IIF. this way you will get the 0 automatically. this for integer or numerical columns. on the other hand set the string column's default value to string.empty.
ASKER
Appreciate all the attention :) And thanks for being there Sancler!
I had a funny feeling about that ds.AcceptChanges(). Removed that.
For newyuppie,
1) Understood
2) I had more to the Try structure, but for now, will remove
3) I am passing updates to a stored procedure. Don't know if a da can take updates and run it through an sp.
Now my update code looks like below (null checking will be changed per newyuppie's suggestion later). Once I removed the Try structure, it revealed an error where the ds.Tables(0).GetChanges returned a NullReferenceException. In other words, if I pressed the Update button when there were no changes, it caused a NullReferenceException. So, by checking "If Not tbl Is Nothing" then it seems to operate correctly. (Don't know if that makes sense).
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
DataGridView1.BindingConte xt(ds.Tabl es(0)).End CurrentEdi t()
Dim tbl As DataTable
tbl = ds.Tables(0).GetChanges
If Not tbl Is Nothing Then
For Each row As DataRow In tbl.Rows
db.CropMasterUpdate(IIf(Is DBNull(row .Item(0)), "", row.Item(0)), _
IIf(IsDBNull(row.Item(1)), "", row.Item(1)), _
IIf(IsDBNull(row.Item(2)), "", row.Item(2)), _
IIf(IsDBNull(row.Item(3)), "", row.Item(3)), _
IIf(IsDBNull(row.Item(4)), "", row.Item(4)), _
IIf(IsDBNull(row.Item(5)), 0, row.Item(5)), _
IIf(IsDBNull(row.Item(6)), 0, row.Item(6)), _
IIf(IsDBNull(row.Item(7)), 0, row.Item(7)), _
IIf(IsDBNull(row.Item(8)), 0, row.Item(8)))
Next
End If
BindGrid()
End Sub
Again, I appreciate your help. Let's hope there isn't a "part III" to this saga.
Regards,
ADWooley
I had a funny feeling about that ds.AcceptChanges(). Removed that.
For newyuppie,
1) Understood
2) I had more to the Try structure, but for now, will remove
3) I am passing updates to a stored procedure. Don't know if a da can take updates and run it through an sp.
Now my update code looks like below (null checking will be changed per newyuppie's suggestion later). Once I removed the Try structure, it revealed an error where the ds.Tables(0).GetChanges returned a NullReferenceException. In other words, if I pressed the Update button when there were no changes, it caused a NullReferenceException. So, by checking "If Not tbl Is Nothing" then it seems to operate correctly. (Don't know if that makes sense).
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
DataGridView1.BindingConte
Dim tbl As DataTable
tbl = ds.Tables(0).GetChanges
If Not tbl Is Nothing Then
For Each row As DataRow In tbl.Rows
db.CropMasterUpdate(IIf(Is
IIf(IsDBNull(row.Item(1)),
IIf(IsDBNull(row.Item(2)),
IIf(IsDBNull(row.Item(3)),
IIf(IsDBNull(row.Item(4)),
IIf(IsDBNull(row.Item(5)),
IIf(IsDBNull(row.Item(6)),
IIf(IsDBNull(row.Item(7)),
IIf(IsDBNull(row.Item(8)),
Next
End If
BindGrid()
End Sub
Again, I appreciate your help. Let's hope there isn't a "part III" to this saga.
Regards,
ADWooley