473,324 Members | 2,417 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,324 software developers and data experts.

DataTable.Rows.Clear question

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();
}
}
}
Mar 9 '06 #1
0 4768

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

Similar topics

5
by: Nathan Sokalski | last post by:
I am writing an ASP.NET application in which I need to copy DataRows from one DataTable to another. When I use code such as the following: temprows = nodes.Select("state='PA'")...
4
by: slaprade | last post by:
I am loading a weeks worth of web logs into a dataset using Imports Microsoft.Data.Odbc These are text - fixed length fields so I have written a schema for them. The adapter fill looks like this...
1
by: Simon | last post by:
Hi All, I have a DataGrid bound to a DataTable. Every once in a while I need to refresh the data in the table so that the updated data is displayed in the DataGrid. Here's how I am attempting...
6
by: Danny Ni | last post by:
Hi, If I want to programatically add rows to a DataTable, do I call AcceptChanges per row? Or do I call AcceptChanges after all rows added? TIA
3
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate...
7
by: Susan Mackay | last post by:
I have a data table that is connected to a database table with a data adapter in the 'standard' manner. However I want to be able to remove selected rows from the data table (i.e. no longer...
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to...
1
by: tshad | last post by:
Running on VS.net 2005, I am trying to copy rows from my datatable to another datatable in the same dataset. The schema would be identical. I need to make the table name "forms" as I am...
9
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.