Saint Saint - 1 year ago 54
C# Question

How to fire DataTableNewRowEvent after Rows.Add

Now I have

var row = container.Tables[name].NewRow();
row[0] = time;
row[1] = val;
container.Tables[name].Rows.Add(row);


Of course event
DataTableNewRowEvent
is fired in first line. But I need fire them after last line, I mean after adding new row to
RowsCollection
. How to make it? I need it to get actual data in
DataTable
at the moment of firing
DataTableNewRowEvent


I think about:

//to insert proper data
var row = container.Tables[name].NewRow();
row[0] = time;
row[1] = val;
container.Tables[name].Rows.Add(row);
//to fire event
container.Tables[name].Rows.Remove(container.Tables[name].NewRow());


but probably it's not best solution.

Answer Source

I think you better handle the RowChanged event on DataTable, you'll get a parameter of type DataRowChangeEventArgs that has a DataRowAction property that let's you know the change that actually took place on the row, Add being one of the possible values.

Your handler would look like this

protected void DataTable_RowChanged(object sender, DataRowChangeEventArgs e)
{
    if (e.Action == DataRowAction.Add)
    {
        // Do something with e.Row which is already populated
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download