I have a C# windows forms application with a form which
opens a
non-modal form containing a data grid. The datagrid's
datasource is a
DataTable. If the datatable's AcceptChanges method is
called, the
non-modal form is moved to the front in Z-order. Is there
a way to
prevent this? I have tried setting the datagrid's
dataSource to null,
before calling AcceptChanges as well as calling BeginInit
and
BeginLoadData before AcceptChanges, but with no success.

I have also noted that setting the DataSource on the
DataGrid to null will also cause the Z-order issue.

Any ideas for work arounds?


The sample code is included:

Form1 contains 2 buttons. One to open the non-modal Form2
and one to
call a public method on Form2 which calls Form2's data
table's
AcceptChanges method.
Form2 contains a simple data grid with a datatable with
one column and
one row.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after
InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer
(this.components);
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler
(this.timer1_Tick);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(48,
80);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(32,
184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 56);
this.button1.TabIndex = 1;
this.button1.Text = "Show DataGrid Form";
this.button1.Click += new System.EventHandler
(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(144,
184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 56);
this.button2.TabIndex = 2;
this.button2.Text = "Data Table Accept Changes";
this.button2.Click += new System.EventHandler
(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void timer1_Tick(object sender, System.EventArgs
e)
{
System.Diagnostics.Trace.WriteLine("Form
Focused="+this.Focused.ToString()+"
TBfocused="+textBox1.Focused.ToString());
}

private Form2 m_Frm2;
private void button1_Click(object sender,
System.EventArgs e)
{
if (m_Frm2==null || m_Frm2.Visible==false)
{
m_Frm2 = new Form2();
m_Frm2.Show();
}
}

private void button2_Click(object sender,
System.EventArgs e)
{
if (m_Frm2!=null)
m_Frm2.DataTableAcceptChanges();
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataTable m_DataTable;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components =
null;

public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

m_DataTable = new DataTable("MyTable");
m_DataTable.Columns.Add("Col");
m_DataTable.Rows.Add(new string[] {"A"});
dataGrid1.DataSource = m_DataTable;
}

public void DataTableAcceptChanges()
{
m_DataTable.AcceptChanges();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
} base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)
(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(72,
40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(408, 216);
this.dataGrid1.TabIndex = 0;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(632, 365);
this.Controls.Add(this.dataGrid1);
this.Name = "Form2";
this.Text = "Form2";
((System.ComponentModel.ISupportInitialize)
(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

} #endregion
}
}