سؤال

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.

هل كانت مفيدة؟

المحلول

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
    }
}

نصائح أخرى

DataTableNewRowEvent event is fired inside

protected virtual void OnTableNewRow(DataTableNewRowEventArgs e);

This method is called only in NewRow method of DataTable class. If you want to fire your event at a different time the only solution except inheriting DataTable class would be to use reflection.

MethodInfo mi = (typeof(DataTable)).GetMethod("OnTableNewRow", BindingFlags.Instance | BindingFlags.NonPublic);

// ...

mi.Invoke(container.Tables[name], new object[] { new DataTableNewRowEventArgs(row) }); // fire the event as you please
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top