datagrid doesn't reflect addnew to dataset

G

Guest

datagrid doesn't reflect addnew to dataset

I have a form which includes a currency manager (cm), a dataset with one table, and a set of controls that shows the table one row at a time. The buttons allow edit, delete, addnew, update the dataset, and submitchanges (update the dataadapter)

The form also includes a grid whose datasource is the dataset's one table. There is no relation between the two except two different views of the same table. The grid is readonly, parent rows are hidden. I had no problem using the mousedown event and hittest method of the grid, and the position properties of the grid and the currency manager to keep the rows in sync

I am having a big problem, however. If I edit a row or delete a row, and then update the dataset, these are reflected in the grid. However, if I add a row, it is not reflected. Even after I update the dataadapter the new row doesn't appear

Can someone help me on this

polynomial5d
 
C

Cor Ligthert

Hi Poly,

I think the best to get help from us is show some code where you add the row
to the datatable, because normaly it would be showed.

Cor
 
G

Guest

Cor

You're right, sorry. I'm putting in the code of all the relevant procedures so you and others can put the add into perspective

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Clic
cm.AddNew(
SetEditMode(True
End Su

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Clic
If cm.Count > 0 The
cm.RemoveAt(cm.Position
Els
MessageBox.Show("No Item to Delete!", "Delete Item", MessageBoxButtons.OK, MessageBoxIcon.Error
End I
End Su

Private Sub btnSubmitChanges_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitChanges.Clic
If DsDT1.HasChanges The
DTypeOpened = Fals
Tr
Dim intModified As Intege
intModified = daDT.Update(DsDT1.DateType
Dim strOutput As Strin
strOutput = "Modified " & intModified & " item(s)
MessageBox.Show(strOutput, "Update succeeded!", MessageBoxButtons.OK, MessageBoxIcon.Information
DTypeOpened = Tru
Dim s = ControlChars.CrL
Catch ex As OleDbExceptio
If ex.Errors(0).SQLState = 3022 The
MsgBox(ex.Errors(0).Message & s & "Please try again"
DsDT1.Clear(
daDT.Fill(DsDT1, "DateType"
Exit Tr
Els
Dim errorMessages As Strin
errorMessages += "Message: " & ex.Errors(0).Message & ControlChars.CrLf
& "NativeError: " & ex.Errors(0).NativeError & ControlChars.CrLf
& "Source: " & ex.Errors(0).Source & ControlChars.CrLf
& "SQLState: " & ex.Errors(0).SQLState & ControlChars.CrLf
& "The form will be closed
MsgBox(errorMessages
Me.Close(
End I
Catch ex As DBConcurrencyExceptio
MsgBox(ex.Message & s & "The dataset will be refreshed." & s & "Then you can navigate to the row and update it again."
DsDT1.Clear(
daDT.Fill(DsDT1, "DateType"
Exit Tr
Catch ex As Exceptio
MsgBox(ex.GetType.ToString & s & ex.Message & s & ex.HelpLink & s & ex.StackTrace & s & ex.Source & s & "The form will be closed") '& s & ex.TargetSite
Me.Close(

End Tr
Els
MessageBox.Show("No changes to submit!", "SubmitChanges", MessageBoxButtons.OK, MessageBoxIcon.Information
End I

End Su

Private Sub SetEditMode(ByVal blnEdit As Boolean
'txtID.ReadOnly = Not blnEdi
txtDateType.ReadOnly = Not blnEdi
chkActive.Enabled = blnEdi

btnMoveFirst.Enabled = Not blnEdi
btnMovePrevious.Enabled = Not blnEdi
btnMoveNext.Enabled = Not blnEdi
btnMoveLast.Enabled = Not blnEdi

btnCancel.Enabled = blnEdi
btnUpdate.Enabled = blnEdi
btnEdit.Enabled = Not blnEdi
btnAdd.Enabled = Not blnEdi
btnDelete.Enabled = Not blnEdi
btnSubmitChanges.Enabled = Not blnEdi
End Su

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Clic
If cm.Count > 0 The
SetEditMode(True
Els
MessageBox.Show("No Item to Edit!", "Edit Item", MessageBoxButtons.OK, MessageBoxIcon.Error
End I
End Su


Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Clic
cm.EndCurrentEdit(
SetEditMode(False
End Su

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Clic
cm.CancelCurrentEdit(
SetEditMode(False
End Sub

polynomial5d
 
C

Cor Ligthert

Hi Polynamial,

I have looked to that add routine, you do everything with the concurrency
manager (i asume the cm is the concurrencymanager. However what is binded
to that?

Cor
 
C

Cor Ligthert

And than before I ask a next question, what is the datasource of your
datagrid?

Cor
 
G

Guest

Cor,

I hope by the first question you mean this:

cm = CType(BindingContext(DsDT1, "DateType"), CurrencyManager)
AddHandler cm.ItemChanged, AddressOf cm_ItemChanged
AddHandler cm.PositionChanged, AddressOf cm_PositionChanged

And by the second question you mean this:

DsDT1.DateType

polynomial5d
 
C

Cor Ligthert

Hi Poly,

I never use it like this, however I tried it and got the same behaviour
using the dataset in the currencymanager, when I was using a dataview it
though it did work as wanted.

Can you try it. It is very easy to add.

dim dataview as new dataview(dsdt1.Tables("DateType")
cm = CType(BindingContext(dv), CurrencyManager)
AddHandler cm.ItemChanged, AddressOf cm_ItemChanged
AddHandler cm.PositionChanged, AddressOf cm_PositionChanged

And by the second question you mean this:
And somewhere there has to be now
Datagrid1.datasource = dsdt1.tables("DataType") ' or something like that.
Datagrid1.datasource = dv

When you use the designer than you could drag a dataview to your form and
set the datasource in that as I did in code and than you can set the
dataview in the datagrid as the datasource.

Just to try, for me I thought this did work as wanted.

Cor
 
C

Cor Ligthert

Hi Poly,

There was a problem almost the same as yours in another newsgroup.
I told you I never use the bindingmanager to add a record.
However when I tested again with the bc.addnew I thought that I got
unpredictable results. When you keep having that also, can you than try what
it does when you change that addnew for this.

ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)

where ds is the dataset and 0 the table in the dataset (may also be the
table name between "")

Cor
 
G

Guest

datagrid doesn't reflect addnew to dataset

I have a form which includes a currency manager (cm), a dataset with one table, and a set of controls that shows the table one row at a time. The buttons allow edit, delete, addnew, update the dataset, and submitchanges (update the dataadapter)

The form also includes a grid whose datasource is the dataset's one table. There is no relation between the two except two different views of the same table. The grid is readonly, parent rows are hidden. I had no problem using the mousedown event and hittest method of the grid, and the position properties of the grid and the currency manager to keep the rows in sync

I am having a big problem, however. If I edit a row or delete a row, and then update the dataset, these are reflected in the grid. However, if I add a row, it is not reflected. Even after I update the dataadapter the new row doesn't appear

Can someone help me on this

polynomial5

Hi Poly

I think the best to get help from us is show some code where you add the ro
to the datatable, because normaly it would be showed

Co

Cor

You're right, sorry. I'm putting in the code of all the relevant procedures so you and others can put the add into perspective

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Clic
cm.AddNew(
SetEditMode(True
End Su

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Clic
If cm.Count > 0 The
cm.RemoveAt(cm.Position
Els
MessageBox.Show("No Item to Delete!", "Delete Item", MessageBoxButtons.OK, MessageBoxIcon.Error
End I
End Su

Private Sub btnSubmitChanges_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitChanges.Clic
If DsDT1.HasChanges The
DTypeOpened = Fals
Tr
Dim intModified As Intege
intModified = daDT.Update(DsDT1.DateType
Dim strOutput As Strin
strOutput = "Modified " & intModified & " item(s)
MessageBox.Show(strOutput, "Update succeeded!", MessageBoxButtons.OK, MessageBoxIcon.Information
DTypeOpened = Tru
Dim s = ControlChars.CrL
Catch ex As OleDbExceptio
If ex.Errors(0).SQLState = 3022 The
MsgBox(ex.Errors(0).Message & s & "Please try again"
DsDT1.Clear(
daDT.Fill(DsDT1, "DateType"
Exit Tr
Els
Dim errorMessages As Strin
errorMessages += "Message: " & ex.Errors(0).Message & ControlChars.CrLf
& "NativeError: " & ex.Errors(0).NativeError & ControlChars.CrLf
& "Source: " & ex.Errors(0).Source & ControlChars.CrLf
& "SQLState: " & ex.Errors(0).SQLState & ControlChars.CrLf
& "The form will be closed
MsgBox(errorMessages
Me.Close(
End I
Catch ex As DBConcurrencyExceptio
MsgBox(ex.Message & s & "The dataset will be refreshed." & s & "Then you can navigate to the row and update it again."
DsDT1.Clear(
daDT.Fill(DsDT1, "DateType"
Exit Tr
Catch ex As Exceptio
MsgBox(ex.GetType.ToString & s & ex.Message & s & ex.HelpLink & s & ex.StackTrace & s & ex.Source & s & "The form will be closed") '& s & ex.TargetSite
Me.Close(

End Tr
Els
MessageBox.Show("No changes to submit!", "SubmitChanges", MessageBoxButtons.OK, MessageBoxIcon.Information
End I

End Su

Private Sub SetEditMode(ByVal blnEdit As Boolean
'txtID.ReadOnly = Not blnEdi
txtDateType.ReadOnly = Not blnEdi
chkActive.Enabled = blnEdi

btnMoveFirst.Enabled = Not blnEdit
btnMovePrevious.Enabled = Not blnEdit
btnMoveNext.Enabled = Not blnEdit
btnMoveLast.Enabled = Not blnEdit

btnCancel.Enabled = blnEdit
btnUpdate.Enabled = blnEdit
btnEdit.Enabled = Not blnEdit
btnAdd.Enabled = Not blnEdit
btnDelete.Enabled = Not blnEdit
btnSubmitChanges.Enabled = Not blnEdit
End Sub

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If cm.Count > 0 Then
SetEditMode(True)
Else
MessageBox.Show("No Item to Edit!", "Edit Item", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub



Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
cm.EndCurrentEdit()
SetEditMode(False)
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
cm.CancelCurrentEdit()
SetEditMode(False)
End Sub

Hi Polynamial,

I have looked to that add routine, you do everything with the concurrency
manager (i asume the cm is the concurrencymanager. However what is binded
to that?

Cor

And than before I ask a next question, what is the datasource of your
datagrid?

Cor
And than before I ask a next question, what is the datasource of your
datagrid?

Cor

Cor,

I hope by the first question you mean this:

cm = CType(BindingContext(DsDT1, "DateType"), CurrencyManager)
AddHandler cm.ItemChanged, AddressOf cm_ItemChanged
AddHandler cm.PositionChanged, AddressOf cm_PositionChanged

And by the second question you mean this:

DsDT1.DateType

polynomial5d

Hi Poly,

I never use it like this, however I tried it and got the same behaviour
using the dataset in the currencymanager, when I was using a dataview it
though it did work as wanted.

Can you try it. It is very easy to add.

dim dataview as new dataview(dsdt1.Tables("DateType")
cm = CType(BindingContext(dv), CurrencyManager)
AddHandler cm.ItemChanged, AddressOf cm_ItemChanged
AddHandler cm.PositionChanged, AddressOf cm_PositionChanged
And somewhere there has to be now
Datagrid1.datasource = dsdt1.tables("DataType") ' or something like that.
Datagrid1.datasource = dv

When you use the designer than you could drag a dataview to your form and
set the datasource in that as I did in code and than you can set the
dataview in the datagrid as the datasource.

Just to try, for me I thought this did work as wanted.

Cor

Hi Poly,

There was a problem almost the same as yours in another newsgroup.
I told you I never use the bindingmanager to add a record.
However when I tested again with the bc.addnew I thought that I got
unpredictable results. When you keep having that also, can you than try what
it does when you change that addnew for this.

ds.Tables(0).Rows.Add(ds.Tables(0).NewRow)

where ds is the dataset and 0 the table in the dataset (may also be the
table name between "")

Cor

Cor,

The first two didn't work at all for me. Here's the code:

When I used dgrd.datasource = dsdt1.tables... the grid didn't fill at all.
When I used dgrd.DataSource = dv the grid filled.

However, in both cases when the cm wouldn't cycle. As I moved rows only the first row showed; furthermore when I clicked add the datetype textbox wasn't blank. It still showed the first row, and the datagrid(in the second case) produced a null in the datetype column, inevitably producing an exception unless I clicked cancel.

In the property boxes for the three bound controls, there was of course no option to select dataview.

I'm afraid to use your third option, because the solution would no longer be portable.

polynomial5d



cnDT.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Database Password=;Data Source=""" + System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\HasbaraSample.mdb"";Password=;Jet OLEDB:Engine Type=5;Jet OLEDB:Global Bulk Transactions=1;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:New Database Password=;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Encrypt Database=False"
'cm = CType(BindingContext(DsDT1, "DateType"), CurrencyManager)
Dim dv As New DataView(DsDT1.Tables("DateType"))
cm = CType(BindingContext(dv), CurrencyManager)
dgrd.DataSource = DsDT1.Tables("DataType") ' or something like that.
'dgrd.DataSource = dv
 
G

Guest

Cor, when I tried to use the view, the wizard couldn't generate the update or delete commands becaus

Could not determine which columns uniquely identify the rows for "qryDateTypeAsc"

polynomial5d
 
G

Guest

Cor

just so you know: I got my basic framework from ADO.NET Core Reference, Chapter 13, published by Microsoft Press. I figured if any source would have it right, this one would. Apparently not. I would call this a defect. Can you think of any reason Microsoft would design it so that dataset wouldn't work and dataview would? Especially when the sql is the same

A few weeks ago I had a problem with quirky behavior after filling a dataset. One of the MVP's had me email him the entire solution. It took him several days of debugging, but finally the problem was in the following syntax

didn't work completely: da.fill(ds1.DateType
did work completely: da.fill(ds1, "DateType"

By now both these things, and probably many more must be known to Microsoft. Since Whidbey isn't going to be out until late 2005 or maybe 2006, shouldn't they release a service pack that fixes these issues

polynomial5d
 
C

Cor Ligthert

Hi Polynimial,

The biggest problem for me is that I am not using the Bindingmanager.addnew.
I changed a testprogram however saw afterwards that it was with an
autoincremental number and there was in the addnew a update a fill to get
that number.

Your problem in the same way was in the language.vb group. I asked the one
to change the addnew for the way I was normaly using and the problem was
gone. So instead of searching to the problem with the bindingmanager, maybe
you can try to change this.

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
cm.AddNew()
SetEditMode(True)
End Sub

into
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Ds1.tables("DateType").Rows.Add(Ds1.Tables("DateType").NewRow)
SetEditMode(True)
End Sub

The Ds1 I got from the code you did send in your last update, that is the
non typed datasetname I assume. I asume that is also the problem with the
dataview, however forget that and try this first. When this is not working
than try it with changing that DS1 in DsDt1.

You are using it than in the way I am normaly doing.

I hope this brings us a step further?

Cor
 
P

Pasha Aryana

I had this problem a while back with VB6. The DataGrid should have a
ReBind method. So once you have refreshed the recordset thats linked to
it, then use the DataGrids rebind method to refresh itself and get the
latest data.
 
G

Guest

Cor, the da and ds1 were just substitutes for daDT and DsDT1. The dataset is strongly typed so that isn't the problem. I'll get around to trying your new proposal in about 2 hours since I have a coffee date now. Although my cerebral lobes knows finishing off this problem is more important than a coffee date, my limbic system rules the roost

polynomial5d
 
G

Guest

Cor, I'm afraid it didn't work.

I typed in DsDT1.Tables("DateType").Rows.Add(DsDT1.Tables("DateType").NewRow)

Here's what happened. Everything loads okay. When I click Add the datetype textbox doesn't clear, but a new row appears at the bottom of the grid, with null in the corressponding column. If I click cancel, the cm is okay but the extra row stays in the grid. Either I close the form or click submitchanges, in which case I get an exception sqlstate 3314.

polynomial5d
 
C

Cor Ligthert

I get more and more after what you are doing, you have a row of texboxes on
your form I now understand.
Should be even better with the dataview.
However forget that for a while when you add after that row you insterted
this

cm.Position = dsDT1.Tables(0).Rows.Count -1
Than the new empty row should be in the textbox and the datagrid pointer on
the right row.

Your cm.cancelcurrentedit does with me nothing.
However when I place this in it it cancels all edits .
ds.Tables(0).RejectChanges()

Maybe you can try that, than is the only thing that has to be done is the
writing, and therefore I want to know if you use an autokey or how you make
the key?

Cor
 
G

Guest

It's an autoincremented integer called ID. The 2nd text box is called datetype. The third control is a checkbox, called Active, default is true

Before when I tried dataview, the results didn't work. If I drag a view off the server explorer I don't see how it would be portable

polynomial5d
 
C

Cor Ligthert

Hi Polynomial,

You said that that fill went wrong and that they did help you and changed
it, I think you did not had changes before so the update was no problem,
maybe can you change it in the same way you did the fill
daDT.Fill(DsDT1, "DateType")

intModified = daDT.Update(DsDT1.DateType)
that means
intModified = daDT.Update(DsDT1, "DateType")
and to get the autonumber direct after it
daDT.Fill(DsDT1, "DateType")

and before this
If DsDT1.HasChanges Then

you place
cm.endcurrentedit

I am currious if there is some progression?

Cor
 
G

Guest

Cor

I first want to mention that before I put the grid on the form, the cm worked perfectly. Not only on this form, but on 12 similar forms. It's the addition of the grid that started the problems with the addnew

polynomial5d
 
G

Guest

Hi polynomial5d,

I found a BUG-Report from MS about the addnew-problem. They suggest a workaround that didn't work :-(, but maybe yopu want to have a look on it:


http://support.microsoft.com/default.aspx?scid=kb;en-us;816227

by the way, I have the same problem AND one more with deleting rows in a DG:

My problem:

if a datagrid-row is deleted by the user (DEL), it vanishes, whatever I do!

Even if I undo the action via code in the RowDeleted-Event of the underlying table, the (formerly deleted) row will not show! But in the table itself the row is correctly

updated and undeleted. The only way to bring the row back into the grid seems to click on any header of the grid to sort any column.

An even worse problem is the behaviour with ADDED rows. Those rows show in the datagrid, but are NOT added to the table, so any attempt to update the DB will fail.

More curious the new row seems to belong to the table (I tested is with adding the e.row to the table inside the RowChanged-Event, which causes a row already

exists-error!). MS postet a solution for this bug, but the solution doesn't work either.

Peter
 

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

Top