DataGrid / DataView Problems

  • Thread starter Thread starter NetRacer
  • Start date Start date
N

NetRacer

hi,

i have a DataGrid with a DataView as source. the data is read correctly from
the db and shown on screen.
the problem appears, when i want to add some rows to the grid.

first it adds them and the ListChangedEvent from the DataView raises and the
new values are shown correctly.
but if i then click on the save button (which calls the EndCurrentEdit and
Update methods), the DataView suddenly only has the original rows and the
new ones are lost.

then if i try again to add some values, they are lost imediately when i
leave the current row in the DataGrid.

there are no constraints, keys or other restrictions in the underlaying
DataTable.

a suggestion:
at first addition of the row there raises a strange second ListChangedEvent
after the ItemAdded. the ListChangedType is ItemMoved, then OldIndex equals
the current row and the NewIndex something like -23423435. is my problem
there?

thx for any help
netracer
 
NetRacer,

This is in my opinion impossible to answer without code.
Can you show the code from this event.
but if i then click on the save button (which calls the EndCurrentEdit and
Update methods), the DataView suddenly only has the original rows and the
new ones are lost.

Cor
 
Private Function SaveDataGrids(ByVal grid As DataGrid, Optional ByVal
ShowErrors As Boolean = True) As Boolean

Dim cnt As Integer = 0
Dim dvGrid As DataView

dvGrid = CType(grid.DataSource, DataView)

Dim daGrid As New MySqlDataAdapter(String.Format("SELECT * FROM {0}
", dvGrid.Table.TableName), conn)
Dim cbGrid As MySqlCommandBuilder

Try
cbGrid = New MySqlCommandBuilder(daGrid)

'-----
'if i set a breakpoint here, the record is already lost
'-----
Me.BindingContext(grid.DataSource).EndCurrentEdit()

cnt = daGrid.Update(dvGrid.Table)

'MessageBox.Show(cnt.ToString & " lines from " &
dv.Table.TableName & " updated.", _
'"Datenbankinfo", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Catch ex As MySqlException
If cnt <= 1 And ShowErrors Then
MessageBox.Show(ex.Message, "Datenbankfehler",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

Return (cnt > 0)
End Function
 
ahhh... stop, i found the error. a property set of a form property reloaded
the table from database. *grml*... stupid error
 
NetRacer,
Dim daGrid As New MySqlDataAdapter(String.Format("SELECT * FROM {0}
", dvGrid.Table.TableName), conn)
Dim cbGrid As MySqlCommandBuilder

Try
cbGrid = New MySqlCommandBuilder(daGrid)
You dataadapter has not any schemainformation, how would it create the
commands.

I never did it this way, however you can try the Dataadapter.fillschema in
advance, I assume that it will solve your problem.

http://msdn.microsoft.com/library/d...tacommondbdataadapterclassfillschematopic.asp

There is written that it adds a datatable. Therefore I assume it is not
going 1:1

The datatable that uses your dvGrid is just dvGrid.DataTable

I hope this helps,

Cor
 
the schema is/was not the problem. the command is created correctly.
look the log of MySQL:

"UPDATE `tbl_ersatz_abs` SET `Rekl_ID`=1, `Pos`=4, `ErsatzAB`=1644437,
`ErsatzABText`='@Y', `ZNA_ID`=4 WHERE `E_AB_ID` <=> 5 AND `Rekl_ID` <=> 1
AND `Pos` <=> 4 AND `ErsatzAB` <=> 1644437 AND `ErsatzABText` <=> '@Z' AND
`ZNA_ID` <=> 4; SELECT `E_AB_ID`, `Rekl_ID`, `Pos`, `ErsatzAB`,
`ErsatzABText`, `ZNA_ID` FROM `tbl_ersatz_abs` WHERE (`E_AB_ID`=5)"


if you're not familiar with MySQL: the operator <=> does a 'NULL-safe equal'
comparison.

if i use FillSchema or the AddWithSchema option, the grid annoys me with
popup boxes that i should set an ID. i don't want to set an ID in my
program, i want the server to do this.

by the way the Fill is done much earlier at loading the form.



the problem was a few lines before the posted sub. another datasource was
saved there and at the EndCurrentEdit of this saving a property set was
called which did a new select on the database and so overwrote my changes.

however, thanx for trying to help
netracer
 
NetRacer,

I was misinterpretting your code of MySQL. I thought: "is it MySQL or
YourSQL?
However just a kind of blindness it very clear.

:-))

Sorry

Cor
 
Ähhhm... sorry, but i could not really understand your joke (it was one,
wasn't it?). i'm not a naturally english speaker (as you can see in my bad
english, i think)...

by the way: YourSQL really exists. it's a frontend tool for MySQL ;)

however, thanx again for your try to help
netracer
 
Netracer

You can interpret it as a joke, it was just a stupid mistake from me reading
your code.

:-)

Cor
 

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

Back
Top