Results 1 to 7 of 7

Thread: Detecting AFTER row added to datatable

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Malaysia
    Posts
    64

    Detecting AFTER row added to datatable

    I've been trying to use the RowChanged event of the datatable to detect whenever a row has been updated, deleted or added.

    It works fine for deleted rows and updated rows but doesn't work for added rows.

    It seems the event is fired BEFORE the row is actually added to the datatable. I've checked.

    I need an event or something that fires AFTER the row is added so I can get the newly added row as well from the datatable.

    I really don't get it. If the event is fired BEFORE the row is added, then doesn't the RowChanged event behave absolutely the same as RowChanging event (at least for added records)?

    Is this a BUG? Any way to get around this? I just need an event that fires AFTER a row is added.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I cant fully understand what you trying to say. I tested the rowchanged event of datatable and it actually fires twice when you add a row.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    What I do is use DataTable.ColumnChanged, then in the event code examine e.Row.Item("ColumnName") to see if it's the right column. I don't exactly remember why I got away from DataTable.RowChanged.

    Oh by the way it makes sense that the event fires twice (.ColumnChanged would fire twice also) - adding a row to a table first does DataTable.NewRow, then DataTable.Rows.Add.
    Last edited by Slow_Learner; Apr 23rd, 2003 at 03:17 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Malaysia
    Posts
    64
    I'm quite sure it fires only once. And the event is RowChanged.

    If you do a check of the rows count of the datatable once the event is fired, you'll see that the row have not actually been added to the datatable when the event is fired.

    It's only added AFTER the event is fired.

    The sequence goes like this

    1. I add row to datatable, calling the AddSomethingRow(blahblah)
    2. The RowChanging event is fired
    3. The RowChanged event is fired
    4. Row is actually added to datatable.

    That's wrong,right? I mean the event is called RowChanged for god sake. So it should fire AFTER the row has been added.
    Now i really don't see what's the difference between RowChanging and RowChanged. They both share the same return parameters. They both share the same event arguments and the properties returned by those event arguments are EXACTLY the same.

  5. #5
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I'm quite sure it fires only once.
    I tested again. You are both right and wrong!
    If you use an external data container to add the row, for example a datagrid the event is fired twice, but if you add it directly via code it is fired once. I dont know why!
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  6. #6
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    That may be why I quit using RowChanged and switched to ColumnChanged instead.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    You might find this useful, had to do my own research on this as well and still yet to test it fully, but appears to do the trick:

    In the form load event:

    ' handler for row changed event
    Dim myTable As DataTable
    myTable = CType(DataGrid2.DataSource, DataTable)
    AddHandler myTable.RowChanged, AddressOf Me.myDataTable_Changed

    then the code for the handler:

    Private Sub myDataTable_Changed _
    (ByVal sender As System.Object, ByVal e As System.Data.DataRowChangeEventArgs)
    ' Console.WriteLine("Row Changed", e.Action, _
    ' e.Row.Item(DataGrid2.CurrentCell.ColumnNumber))
    Dim thisrow As Integer = e.Row.Item(DataGrid2.CurrentRowIndex)
    Dim dr1 As DataRow
    If e.Action = DataRowAction.Add Then
    If Not IsDate(dr1("Date")) Then
    err.Text = "Enter a valid Date e.g. 31/03/2003"
    err.Visible = True
    DataGrid3.Select(thisrow)

    etc......

    the intention here is to detect a row added and do editing on it and some other actions, as I said not tested yet but seemed like the approach to take, hope this helps


    have copied the code from an example in help, but it would seem to do what is required

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width