Thanks for the help Tome.
I figured out what was causing the exception to be thrown. If you set the
ReadOnly property of the DataGrid to true:
this.dataGrid1.ReadOnly = true;
then an IndexOutOfRangeException will be thrown when you try to add a row.
The thing is, it will only be thrown when I have the following line:
dataGrid1.Select();
So, in summary, I have two options. I can not select the datagrid before
adding new rows or I can leave readonly as true and set it to false before I
display the grid.
Can anyone tell me why this behavior is so?
Below I pasted a small example that shows what I found
===============================================
using System;
using System.Data;
using System.ComponentModel;
using System.Windows.Forms;
namespace GridTest
{
public class Test : System.Windows.Forms.Form
{
DataTable dt = new DataTable();
private System.ComponentModel.Container components = null;
private System.Windows.Forms.DataGrid dataGrid1;
public Test()
{
InitializeComponent();
CreateMyDataGrid();
AddRows();
}
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.d ataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.AllowSorting = false;
this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
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.ReadOnly = true;
this.dataGrid1.Size = new System.Drawing.Size(224, 160);
this.dataGrid1.TabIndex = 0;
//
// Test
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(242, 176);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.dataGrid1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Test";
this.Text = "Test";
((System.ComponentModel.ISupportInitialize)(this.d ataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Test());
}
private void CreateMyDataGrid()
{
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;
//If the following line is not commented out and dataGrid1.ReadOnly
//is set to true, IndexOutOfBoundsExceptions will be thrown when you
//try to add new rows to the grid. If you need this line and don't
want the
//exceptions to be thrown, set dataGrid1.ReadOnly to false
dataGrid1.Select();
}
private void AddRows()
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 1;
dr[2] = 2;
dt.Rows.Add(dr); //Exception thrown here
/*
dt.BeginLoadData();
DataRow _moveRow = dt.NewRow();
_moveRow[0] = 0;
_moveRow[1] = 1;
_moveRow[2] = 2;
dt.Rows.Add(_moveRow);
dt.EndLoadData(); //Exception thrown here
*/
}
}
}
===============================================
"Tome" wrote:
Flack,
I tried out your code. I added a datagrid to a blank form. I added the private dt and copied your
given code for CreateMyDataGrid() and called it after InitializeComponent. I didn't get any
exceptions. I even tried dataGrid1.Select(0) and it selected the first row, which had nothing in
it.
If you comment out your style sets in the createmydatagrid() function does it work?
---Tome
On Thu, 2 Mar 2006 13:58:31 -0800, "Flack" <Fl***@discussions.microsoft.com> wrote:
Hey guys,
I have a DataGrid and DataTable field in my class :
private ImageDataGrid dataGrid1; //ImageDataGrid extends dataGrid and just
overides OnMouseDown
private DataTable dt = new DataTable("MyTable");
In the classes constructor, after InitializeComponent() I call the following
method:
private void CreateMyDataGrid()
{
dt.Columns.Add(new DataColumn("From Cell"));
dt.Columns.Add(new DataColumn("Go",typeof(int)));
dt.Columns.Add(new DataColumn("To Cell"));
//Set some styles, etc.
dataGrid1.DataSource = dt;
dataGrid1.Select(); //If I comment this line out everything works OK
}
Now, in some other methods that are called later on I try the following:
DataRow _moveRow = dt.NewRow();
_moveRow[0] = _currentCell.Index;
_moveRow[1] = 0;
_moveRow[2] = _nextCell.Index;
dt.Rows.Add(_moveRow);
When that code runs I get the following error:
A first chance exception of type 'System.IndexOutOfRangeException' occurred
in system.windows.forms.dll
Additional information: Index was outside the bounds of the array.
However, if I comment out the last line in CreateMyDataGrid(), the Selecet
line, everything works fine?
Any ideas why this is so?