Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid row values not saved to dataset until row changes?

Status
Not open for further replies.

Blitz

Technical User
Jul 7, 2000
171
US
Is there any way to save a datagrid row without having the current row change? Currently i can change the value of a column of a row in a datagrid, the new value is not reflected in the dataset until the current cell is a different row. I need a method to save the current row which i assume will have to be done with a derived datagrid but i dont know where to start. Any ideas?
 
You don't need to derive a data grid, but there is a little bit of work involved.

What you need to do is update the underlying datasource as the text box of the cell being edited loses focus - the Validating event of the text box is great for this.

First you need to wire up an event handler to the Validating event of each TextBox in a DataGridTextBoxColumn in the datagrid. In the code below, I am assuming you have a custom table style which you are adding gridcolumnstyles to:

Code:
Dim colTest As New DataGridTextBoxColumn
colTest.MappingName = "RequiredColumn"
AddHandler colTest.TextBox.Validating, AddressOf TextBoxValidating
TableStyle.GridColumnStyles.Add(colTest)

Then handle the Validating event. This allows the datasource to be updated whenever the text box validates, which could be the user tabbing to the next cell or clicking off the grid entirely.

Code:
Private Sub TextBoxValidating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

Dim tbc As DataGridTextBoxColumn = CType(DataGrid.TableStyles(0).GridColumnStyles(DataGrid.CurrentCell.ColumnNumber), DataGridTextBoxColumn)

If Not tbc.TextBox Is Nothing AndAlso tbc.TextBox.Modified Then

	'We have to address the correct datasource row, as if a row has been deleted then the datagrid row number will not be the same as the datasource row

	Dim bm As BindingManagerBase = DataGrid.BindingContext(DataGrid.DataSource, DataGrid.DataMember)

	CType(bm.Current, DataRowView).Row.Item(tbc.MappingName) = tbc.TextBox.Text

End If

End Sub

This works for me, hopefully for you too!
 
I was thinking of going that route but what i did instead is set the datagrids datasource to nothing then set it back to the dataset. The grid saves to the dataset before the datasource it cleared. I am still playing around with this to make sur eit is in fact working properly but so far so good.
 
I realize this is an old post, but I would like your help, if possible. I tried the code that SHelton posted, but my tbc.TextBox.Modified is always False. Is there something I am missing?
Thank you,
Andrea
 
What I have done with all my datagrids is the following:

Add these 2 proceedures to a derived datagrid

Public Sub SaveCurrentColumnToDatagridRow()
Dim dgc As DataGridColumnStyle = Me.TableStyles(0).GridColumnStyles(Me.CurrentCell.ColumnNumber)

Me.EndEdit(dgc, Me.CurrentCell.RowNumber, False)

Dim args As New EventArgs
RaiseEvent ColumnSavedToDatagridRow(Me, args)

End Sub


Public Sub SaveDatagridRowToDataset()
Dim bm As BindingManagerBase
bm = BindingContext(DataSource, DataMember)

CType(bm.Current, DataRowView).EndEdit()
End Sub

SaveCurrentColumnToDatagridRow saves the current active cell back to the grid, then SaveDatagridRowToDataset saves the current row back to the dataset. You can call these two procedures to save your datagrid to the dataset at anytime. Hope this helps.
 
you can take out the " Dim args As New EventArgs
RaiseEvent ColumnSavedToDatagridRow(Me, args)"

I just needed an event to be raised when it was called.
 
Blitz,
Thanks for replying. I got my code, which was a little different, to work by adding bm.EndCurrentEdit.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top