I have pasted at the end of this message a small sample program I whipped up
to do some testing.
It's a form with a datagrid and two buttons. Each button clears the
dataTable that is the source of the dataGrid, and then adds five rows.
Button2 is the same as button1 just that it does this in a different thread
(I was testing if I was having some threading issues).
Anyway, it seems that the location of the call to dt.Rows.Clear() decides if
the app crashes or not. If you only leave one dt.Rows.Clear() call
uncommented you can see what I mean.
There are four calls to Clear, labeled 0 - 3. Only #3 seems to work ok.
Here is how to crash it:
- Make sure only one Clear() call is uncommented (whichever one you want to
test)
- Start the app
- Hit either button 1 or 2
- Select a row thats not row 0 (the one it starts on)
- Hit the same button again (whichever one you hit before)
This crashes the app with an IndexOutOfBounds error. It is likely that I am
misunderstanding something about either DataGrids/DataTables/the connection
between the dataGrid and the dataTable.
Any ideas?
Thanks,
-Flack
================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using System.Data;
namespace FloorsGridTest2
{
public class Form1 : System.Windows.Forms.Form
{
DataTable dt = new DataTable("MyTable");
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
CreateDataTable();
}
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();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(296, 192);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 208);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void CreateDataTable()
{
dt.Columns.Add(new DataColumn("From Cell"));
dt.Columns.Add(new DataColumn("Go",typeof(string)));
dt.Columns.Add(new DataColumn("To Cell"));
dataGrid1.DataSource = dt;
}
private void FillDataGrid()
{
//System.Diagnostics.Debug.Assert(!this.InvokeRequir ed,
"InvokeRequired");
if (InvokeRequired)
{
BeginInvoke(new MethodInvoker(FillDataGrid));
return;
}
//dt.Rows.Clear(); //2
dt.BeginLoadData();
dt.Rows.Clear(); //3
for(int i = 0; i < 5; i++)
{
DataRow moveRow = dt.NewRow();
moveRow[0] = "0";
moveRow[1] = "1";
moveRow[2] = "2";
dt.Rows.Add(moveRow);
}
dt.EndLoadData();
}
private void button1_Click(object sender, System.EventArgs e)
{
//dt.Rows.Clear(); //0
FillDataGrid();
}
private void button2_Click(object sender, System.EventArgs e)
{
//dt.Rows.Clear(); //1
Thread solveThread = new Thread(new ThreadStart(FillDataGrid));
solveThread.Name = "Solve";
solveThread.IsBackground = true;
solveThread.Start();
}
}
}