DataGrid EndCurrentEdit Problem persists...

J

Jan Nielsen

Hi
I asked this question some weeks ago, but have found no soluion yet, so I
try again

I have a parent detail relationship between two tables.
The detail records are shown in a datagrid (GridRemarks)
It is databound like this

GridRemarks.Datasource = mainDatasource ' a shortcut to
myDataSet.tblPersons.DefaultView
GridRemarks.DataMember ="tblPersons_tblRemarks" ' The name of the parent
detail relation
And it works fine

THE PROBLEM: But before I save I would like to end the current edits.
So I tried like this
Dim bmRemarks As BindingManagerBase = BindingContext(mainDataSource,
"tblPersons_tblRemarks")
bmRemarks.EndCurrentEdit()

And like this
Me.BindingContext(GridRemarks.DataSource,
GridRemarks.DataMember).EndCurrentEdit()

But to no avail.
I do not get any errors. It just does not end the current edit.
And my Dataset.HasChanges test is (of course) negative

Now I have tried to write code like this:
Dim myDataGridCell As DataGridCell = Me.GridRemarks.CurrentCell
Dim bm As BindingManagerBase = _
BindingContext(Me.GridRemarks.DataSource, Me.GridRemarks.DataMember)
Dim drv As DataRowView = CType(bm.Current, DataRowView)
Debug.WriteLine("Værdi: " & drv(myDataGridCell.ColumnNumber))
Debug.WriteLine("Row: " & myDataGridCell.RowNumber)

And it works fine and writes the correct values to the debug window

So I guess the bindingmanagerbase is correct. It just will not end the
currentedit

Any suggestions?

TIA

Jan
 
K

Ken Tucker [MVP]

Hi,

Take a look at the datagrids endedit method.
http://msdn.microsoft.com/library/d...stemwindowsformsdatagridclassendedittopic.asp

Ken
----------------------
Hi
I asked this question some weeks ago, but have found no soluion yet, so I
try again

I have a parent detail relationship between two tables.
The detail records are shown in a datagrid (GridRemarks)
It is databound like this

GridRemarks.Datasource = mainDatasource ' a shortcut to
myDataSet.tblPersons.DefaultView
GridRemarks.DataMember ="tblPersons_tblRemarks" ' The name of the parent
detail relation
And it works fine

THE PROBLEM: But before I save I would like to end the current edits.
So I tried like this
Dim bmRemarks As BindingManagerBase = BindingContext(mainDataSource,
"tblPersons_tblRemarks")
bmRemarks.EndCurrentEdit()

And like this
Me.BindingContext(GridRemarks.DataSource,
GridRemarks.DataMember).EndCurrentEdit()

But to no avail.
I do not get any errors. It just does not end the current edit.
And my Dataset.HasChanges test is (of course) negative

Now I have tried to write code like this:
Dim myDataGridCell As DataGridCell = Me.GridRemarks.CurrentCell
Dim bm As BindingManagerBase = _
BindingContext(Me.GridRemarks.DataSource, Me.GridRemarks.DataMember)
Dim drv As DataRowView = CType(bm.Current, DataRowView)
Debug.WriteLine("Værdi: " & drv(myDataGridCell.ColumnNumber))
Debug.WriteLine("Row: " & myDataGridCell.RowNumber)

And it works fine and writes the correct values to the debug window

So I guess the bindingmanagerbase is correct. It just will not end the
currentedit

Any suggestions?

TIA

Jan
 
J

Jan Nielsen

Hi Cor
Thanks for answering (again again)
Unfortunately this does not help.
I'll stick with Kens solution.
I have tried a lot of possible combinations of
bindingcontext.EndCurrentEdit() and none of them worked.
It seems to me that if you use EndCurrentEdit on a datagrids bindingcontext,
it does not end the current edit in the datagrid.
It looks to me like you have to use datagrid.EndEdit()

Best regards

Jan
 
J

Jan Nielsen

I tried this in another project where the datagrid was bound direct to a
single table and there it worked fine!
I tested a bit further and found out that it works if I push _a button_ with
the
BindingContext(ds, "ParentTableName.RelationName"),
CurrencyManager).EndCurrentEdit()
code
But it does not work if it is called from my _toolbarbutton_. Maybe because
the toolbarbutton does not remove focus from the datagrid?


Jan
 
C

Cor Ligthert

Jan,

I tested this and in my opinion it did work, here is the test, it needs
three datagrids and a button on a form.
\\\
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
CreateDs(ds)
DataGrid1.DataSource = ds
DataGrid2.DataSource = ds
DataGrid3.DataSource = ds.Tables("Persons")
DataGrid1.DataMember = "Countries"
DataGrid2.DataMember = "Countries.RPersons"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
DirectCast(BindingContext(ds, "Countries.RPersons"),
CurrencyManager).EndCurrentEdit()
End Sub

'To have a testable class
Private Sub CreateDs(ByVal ds As DataSet)
Dim dtName As New DataTable("Persons")
Dim dcName As New DataColumn("Name")
Dim dcCountry As New DataColumn("Country")
Dim dcState As New DataColumn("State")
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountry)
dtName.Columns.Add(dcState)
ds.Tables.Add(dtName)
For i As Integer = 0 To 5
Dim dr As DataRow = dtName.NewRow
dtName.Rows.Add(dr)
Next
dtName.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU", "Austria"}
dtName.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US", "Florida"}
dtName.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US", "Illinois"}
dtName.Rows(3).ItemArray = New Object() _
{"Terry Burns", "EU", "UK"}
dtName.Rows(4).ItemArray = New Object() _
{"Jay B Harlow", "US", "New York"}
dtName.Rows(5).ItemArray = New Object() _
{"Cor Ligthert", "EU", "Holland"}
Dim dtCountry As New DataTable("Countries")
dcCountry = New DataColumn("Country")
dtCountry.Columns.Add(dcCountry)
ds.Tables.Add(dtCountry)
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountry.NewRow
dtCountry.Rows.Add(dr)
Next
dtCountry.Rows(0)(0) = "EU"
dtCountry.Rows(1)(0) = "US"
Dim drlStates As New DataRelation _
("RPersons", ds.Tables("Countries").Columns("Country"), _
ds.Tables("Persons").Columns("Country"))
ds.Relations.Add(drlStates)
End Sub
////

I hope this helps,

Cor


"Jan Nielsen" <[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top