473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3526
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 maintenance of data. I use separate logic to delete...
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. The problem is that while removing dynamically...
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, but I am not quite sure how to do that. Is that...
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 project has me kinda stumped and after a couple...
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 di = new...
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 tabs to be able to switch back and see whatever...
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 it reaches the bottom of the grid. Is there a way...
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 control (doesn't have to be a grid control, whatever...
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. This will pick up the headers necessary for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.