using GetChanges on a datatable

S

Sam

Hi,
I have a datagrid which source is a dataset :

dgFields.Rows.DataMember = m_dsFields.Tables(0).TableName
dgFields.Rows.DataSource = m_dsFields

Now my values are displayed properly and when I add a row I call the
follwing :

Dim dtChanged As New DataTable
dtChanged = m_dsFields.Tables(0).GetChanges(DataRowState.Added)

but dtChanged is always equal to Nothing.

What am I doing wrong ?

thx
 
C

Cor Ligthert

Sam,

Can you show how you show us how you add the datarow, yesterday there was
somebody who thought he added it, however that was not done, there was alone
a new datarow created.

Cor
 
S

Sam

humm...
To be honnest I don't add anything. At the bottom of my datagrid there
is a new line so I fill in the txtboxes and that's it. I would assume
then that the changes made to the datagrid are reflected on to the
datasource, but I might be wrong.

The code above is all I do. What is missing ?
 
S

Sam

Actually it might be important that I mention that I've tried the exact
same code with the parameters DataRowState.Modified and
DatarowState.Deleted, and that in both cases, my dtChanged is filled
with the right lines !!!!
Therefore I believe my code is correct, however something is wrong with
DatarowState.Added or I'm missing something here !
 
C

Cor Ligthert

Sam,

You add probably a row using the datagrid.

Set this before your get changes than probably it will go.

\\\
BindingContext(m_dsFields.Tables(0)).EndCurrentEdit
///
This pushes the changes from the datagrid into the dataset when there has
not been a rowchange.


I hope this helps,

Cor
 
S

Sam

Cor,
I've done that:
Dim dtChanged As New DataTable
BindingContext(m_dsFields.Tables(0)).EndCurrentEdit()
dtChanged = m_dsFields.Tables(0).GetChanges(DataRowState.Added)

But dtChanged is still Nothing :( I really don't understand why. That's
a pain !
What does BindingContext here ?
 
S

Sam

Cor,
Actually I've noticed that it works with DataRowState.Modified. I mean
that even newly added rows are contained in dtChanged. I guess I can
use that if I can't get the Added method to work
 
C

Cor Ligthert

Sam,

Try this sample partially with your code, I had not any problem, it works
in my opinion as you and I would expect.


\\\needs a datagrid and a button on a form
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("Place")
dt.Columns.Add("Name")
dt.Columns.Add("Key")
dt.LoadDataRow(New Object() {"Whatever", "Sam", "1"}, True)
dt.LoadDataRow(New Object() {"Whatelse", "Cor", "2"}, True)
DataGrid1.DataSource = dt
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dtChanged As New DataTable
BindingContext(dt).EndCurrentEdit()
dtChanged = dt.GetChanges(DataRowState.Added)
DataGrid1.DataSource = Nothing
DataGrid1.DataSource = dtChanged
End Sub
///
I hope this helps,

Cor
 
S

Sam

Thx Cor,
unfortunately it doesn't work for me. My dtChanged is still Nothing....
I'm giving up, I will use DataRowState.Modified instead...
 
C

Cor Ligthert

Sam,
unfortunately it doesn't work for me. My dtChanged is still Nothing....
I'm giving up, I will use DataRowState.Modified instead...
Did you try my sample, because it shows that dtChanged in the datagrid.

Cor
 
S

Sam

well I've tried your code in my program :

Dim dtChanged As New DataTable
BindingContext(m_dsFields.Tables(0)).EndCurrentEdit()

dtChanged = m_dsFields.Tables(0).GetChanges(DataRowState.Added)
dgFields.Rows.DataSource = Nothing
dgFields.Rows.DataSource = dtChanged

and unfortunately dtChanged still is Nothing !
 
S

Sam

I'd like to know what you mean by 'adding a row'.
I add stuff in my cells of the last row of my datagrid (the one marked
by a *)
From the moment I add things in this row, I would assume that the
datasource of this grid should be modified (in fact
DataRowState.Modified works) but apparentely it doesn't detect an added
row

Sam.
 
S

Sam

Because I trust you, I know your code is working, but the thing is that
my code is the same as yours !
 
C

Cor Ligthert

Sam,

There is a new Pope, that is not a miracle, so what is the miracle here.

What Net version are you using.

Cor
 
S

Sam

Cor,
I've tried that but dtChanged is still nothing.
my code :

Dim dtChanged As New DataTable
BindingContext(m_dsFields.Tables(0)).EndCurrentEdit()
dtChanged = m_dsFields.Tables(0).GetChanges(DataRowState.Added)
dgFields.Rows.DataSource = Nothing
dgFields.Rows.DataSource = dtChanged

The thing to note here is that m_dsFields contains the new row ! But
dtChanged does not. So this is weird. Maybe I should have mentionned
that I'm not using the microsoft datagrid but a third party control
(FlyGrid). However this control should not have any influence on
DataRowState.Added and as I said, m_dsFields is properly modified
anyway.
 
C

Cor Ligthert

Sam,

Why not try it with a normal grid and otherwise.
dtChanged = m_dsFields.Tables(0).GetChanges(DataRowState.Added) messagebox.show(dtChanged.Rows.Count.ToString)

dgFields.Rows.DataSource = Nothing
dgFields.Rows.DataSource = dtChanged

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

Top