dataTable.Row is not in Rows Collection at time of RowChanged Event???

G

Guest

Run the program below to duplicate this..

When you enter in a row (enter test in both fields in datagrid and press enter)
The dataTable.RowChanged event is triggered,
the action is Add

but the row is not in the DataTable at this time????????

How in the heck can the view have the row and the dataTable not when they are both viewing state of CurrentRow????

Does it make any sense that an event triggers as a result of the addition of a row to a dataTable but
the row is not in the dataTable at the time of triggering event???

Now I could understand this if this was attached to the dataTable.RowChanging event where the change was
about to happen.

Am I missing something very obvious here??

What I want to do is be able to reprocess the dataTable at the triggering of an event in an application, but when I try to do it the dataRow is not there even though it was added(which of course caused the triggering event in the first place).

//Code that shows what is happening
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DataTableEventTest
{
public class Form1 : System.Windows.Forms.Form
{
DataTable dataTable;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
dataTable = new DataTable("Test");
dataTable.Columns.Add("Name",typeof(System.String) );
dataTable.Columns.Add("YadaYada",typeof(System.String) ) ;
dataGrid1.DataSource = dataTable;
dataTable.RowChanged +=new DataRowChangeEventHandler(dataTable_RowChanged);
dataTable.RowDeleted +=new DataRowChangeEventHandler(dataTable_RowChanged);

this.richTextBox1.Text = String.Empty;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(280, 176);
this.dataGrid1.TabIndex = 1;
//
// richTextBox1
//
this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.richTextBox1.Location = new System.Drawing.Point(8, 192);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.ReadOnly = true;
this.richTextBox1.Size = new System.Drawing.Size(272, 128);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
this.richTextBox1.WordWrap = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 326);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void dataTable_RowChanged(object sender, DataRowChangeEventArgs e)
{
String NL = Environment.NewLine;
DataView dv = new DataView(e.Row.Table);
dv.RowStateFilter = DataViewRowState.CurrentRows;
this.richTextBox1.Text+="view count: "+dv.Count.ToString()+NL;
this.richTextBox1.Text+="passed Table Default View RowStateFilter: "+e.Row.Table.DefaultView.RowStateFilter.ToString()+NL;
this.richTextBox1.Text+="passed row Table row count: "+e.Row.Table.Rows.Count.ToString()+NL;
this.richTextBox1.Text+="data Table row count: "+dataTable.Rows.Count.ToString()+NL;
this.richTextBox1.Text+=e.Row[0].ToString()+":"+e.Action+NL;
}
}
}
 
M

mgwalm

Mike

These events are problematic.

From what ive been able to figure out, the row is not added/changed in
the datatable until after all events are processed, ie till after the
RowChanged evet returns to the framework.

Therefore what i'm now doing, if i want to do anything after the row
is added, is to start a timer in the RowChanged event then do it in
the timer event handler. Eg, i want to commit the row to the database
immediatley it is changed. I call the dataadpated.UpdateChanges in the
time handler.

Regards
Michael
 

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