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 IndexOutOfRange Exception 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.Selec t();
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.Componen tModel;
using System.Windows. Forms;
namespace GridTest
{
public class Test : System.Windows. Forms.Form
{
DataTable dt = new DataTable();
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.DataGrid dataGrid1;
public Test()
{
InitializeCompo nent();
CreateMyDataGri d();
AddRows();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.dataGrid1 = new System.Windows. Forms.DataGrid( );
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .BeginInit();
this.SuspendLay out();
//
// dataGrid1
//
this.dataGrid1. AllowSorting = false;
this.dataGrid1. BorderStyle = System.Windows. Forms.BorderSty le.None;
this.dataGrid1. DataMember = "";
this.dataGrid1. HeaderForeColor =
System.Drawing. SystemColors.Co ntrolText;
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.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.BackColor = System.Drawing. Color.White;
this.ClientSize = new System.Drawing. Size(242, 176);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.dataGrid1} );
this.FormBorder Style = System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "Test";
this.Text = "Test";
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .EndInit();
this.ResumeLayo ut(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run (new Test());
}
private void CreateMyDataGri d()
{
dt.Columns.Add( new DataColumn("Fro m Cell"));
dt.Columns.Add( new DataColumn("Go" ,typeof(string) ));
dt.Columns.Add( new DataColumn("To Cell"));
dataGrid1.DataS ource = dt;
//If the following line is not commented out and dataGrid1.ReadO nly
//is set to true, IndexOutOfBound sExceptions 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.ReadO nly to false
dataGrid1.Selec t();
}
private void AddRows()
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 1;
dr[2] = 2;
dt.Rows.Add(dr) ; //Exception thrown here
/*
dt.BeginLoadDat a();
DataRow _moveRow = dt.NewRow();
_moveRow[0] = 0;
_moveRow[1] = 1;
_moveRow[2] = 2;
dt.Rows.Add(_mo veRow);
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 CreateMyDataGri d() and called it after InitializeCompo nent. I didn't get any
exceptions. I even tried dataGrid1.Selec t(0) and it selected the first row, which had nothing in
it.
If you comment out your style sets in the createmydatagri d() function does it work?
---Tome
On Thu, 2 Mar 2006 13:58:31 -0800, "Flack" <Fl***@discussi ons.microsoft.c om> 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("MyTa ble");
In the classes constructor, after InitializeCompo nent() I call the following
method:
private void CreateMyDataGri d()
{
dt.Columns.Add( new DataColumn("Fro m Cell"));
dt.Columns.Add( new DataColumn("Go" ,typeof(int)));
dt.Columns.Add( new DataColumn("To Cell"));
//Set some styles, etc.
dataGrid1.DataS ource = dt;
dataGrid1.Selec t(); //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.In dex;
_moveRow[1] = 0;
_moveRow[2] = _nextCell.Index ;
dt.Rows.Add(_m oveRow);
When that code runs I get the following error:
A first chance exception of type 'System.IndexOu tOfRangeExcepti on' 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 CreateMyDataGri d(), the Selecet
line, everything works fine?
Any ideas why this is so?