RowChanged event

  • Thread starter Thread starter Aaron Smith
  • Start date Start date
A

Aaron Smith

If I add a handler to the row changed event of a datatable, it fires
when a row has been added, but when in the event, it hasn't actually
added the row yet... Is there a way around this? How can I make sure
that the row has been added to the dataset during this event? Or is
there another one I have to hook into?

Thanks,
Aaron
 
This only fires once the row is added, otherwise it would not have the value
in it )

Private WithEvents myTable As New DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim r As DataRow

Dim c As New DataColumn

r = myTable.NewRow()

r(0) = "Row Added"

myTable.Rows.Add(r)



End Sub

Private Sub myTable_RowChanged(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs) Handles myTable.RowChanged

Debug.WriteLine(e.Row()(0).ToString)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

myTable.Columns.Add("Hello")

End Sub
 
That is only partially correct. e.Row has the data, but while in the
event, the dataset does not have the row... So, inside that event,
calling any kind of update routine that uses the dataset, such as
updating the data source, it will not have any new rows in it.

In your RowChanged event handler, do a writeline on the number of rows
in the datatable... it won't include the new row..
This only fires once the row is added, otherwise it would not have the value
in it )

Private WithEvents myTable As New DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim r As DataRow

Dim c As New DataColumn

r = myTable.NewRow()

r(0) = "Row Added"

myTable.Rows.Add(r)



End Sub

Private Sub myTable_RowChanged(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs) Handles myTable.RowChanged

Debug.WriteLine(e.Row()(0).ToString)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

myTable.Columns.Add("Hello")

End Sub
 
Yes, but it needs to have been commited to acheive this, check this modified
code out



Dim r As DataRow

Dim c As New DataColumn

r = myTable.NewRow()

r(0) = "Screw You"

myTable.Rows.Add(r)

myTable.AcceptChanges()



End Sub

Private Shared Sub myTable_RowChanged(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs) Handles myTable.RowChanged

Dim m As DataTable = CType(sender, DataTable)

If e.Action = DataRowAction.Commit Then

Debug.WriteLine(m.Rows.Count.ToString)

End If

End Sub






Aaron Smith said:
That is only partially correct. e.Row has the data, but while in the
event, the dataset does not have the row... So, inside that event,
calling any kind of update routine that uses the dataset, such as
updating the data source, it will not have any new rows in it.

In your RowChanged event handler, do a writeline on the number of rows
in the datatable... it won't include the new row..
 
PS, ignore the word 'Screw You' in the code, I often write stuff like that
when Im testing, it keeps me happy


Aaron Smith said:
That is only partially correct. e.Row has the data, but while in the
event, the dataset does not have the row... So, inside that event,
calling any kind of update routine that uses the dataset, such as
updating the data source, it will not have any new rows in it.

In your RowChanged event handler, do a writeline on the number of rows
in the datatable... it won't include the new row..
 
Ok, but does that only get called when an Update is being called on the
dataadapter? If it is, then that kind of defeats what I was trying to do
there... What I was ultimately trying to have happen was have the
datasource be updated with the changes everytime a row was changed... I
think I have it figured out with the position event of the
bindingcontext... But I would like to figure out a way to do it with
rowchanged.... I guess it's 6 of 1, half dozen of another. If I can get
it work one way, I should quit complaining. lol
Yes, but it needs to have been commited to acheive this, check this modified
code out



Dim r As DataRow

Dim c As New DataColumn

r = myTable.NewRow()

r(0) = "Screw You"

myTable.Rows.Add(r)

myTable.AcceptChanges()



End Sub

Private Shared Sub myTable_RowChanged(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs) Handles myTable.RowChanged

Dim m As DataTable = CType(sender, DataTable)

If e.Action = DataRowAction.Commit Then

Debug.WriteLine(m.Rows.Count.ToString)

End If

End Sub
 
Thats ok. I laughed when I saw it because I did something similar in one
of the data tables I was working in. The boss happened to run that
program to see if I made any changes to the screen.... he came to my
cube and said, "Having a bad day?" lol
 
Yes your right, I dont think you will be able to do what your after with the
RowChange event, the Add action allows last minute validation, it seems to
have been designed this way.

As you say, the binding contect route is probably the way 2 go

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
Aaron Smith said:
Ok, but does that only get called when an Update is being called on the
dataadapter? If it is, then that kind of defeats what I was trying to do
there... What I was ultimately trying to have happen was have the
datasource be updated with the changes everytime a row was changed... I
think I have it figured out with the position event of the
bindingcontext... But I would like to figure out a way to do it with
rowchanged.... I guess it's 6 of 1, half dozen of another. If I can get
it work one way, I should quit complaining. lol
 
Ok, thanks Terry.. I guess I just needed verification.. I'm just
starting out at this and trying to get used to everything... The Data
Form Wizard sure does a lot of strange things.... I ended up commenting
out the UpdataDataSet routine in there and just putting in
dataadapter.Update ... Seems to work a little cleaner for some reason...
Yes your right, I dont think you will be able to do what your after with the
RowChange event, the Add action allows last minute validation, it seems to
have been designed this way.

As you say, the binding contect route is probably the way 2 go

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
 

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