472,328 Members | 1,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Trouble with DataGrid and adding Rows

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?

Mar 2 '06 #1
2 3441
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?

Mar 3 '06 #2
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?

Mar 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet....
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer,...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest...
2
by: Praveen Balanagendra via .NET 247 | last post by:
here is the source code private void AddRow() { TableCell tc = new TableCell(); tc.Controls.Add(new LiteralControl("NewRow")); DataGridItem...
16
by: scorpion53061 | last post by:
Well as some of you know I was using a tab control for a project I was building for my boss. Today he tells me that he wants: When he switches...
10
by: Aaron Smith | last post by:
If I have a datagrid and the bound data file only have 4 rows in it, the grid will show the 4 rows. However, there is blank space below that until...
13
by: shookim | last post by:
I don't care how one suggests I do it, but I've been searching for days on how to implement this concept. I'm trying to use some kind of grid...
12
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row....
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.