473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 IndexOutOfBound s error. It is likely that I am
misunderstandin g something about either DataGrids/DataTables/the connection
between the dataGrid and the dataTable.

Any ideas?

Thanks,
-Flack

=============== =

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Threadin g;
using System.Windows. Forms;
using System.Data;

namespace FloorsGridTest2
{
public class Form1 : System.Windows. Forms.Form
{
DataTable dt = new DataTable("MyTa ble");
private System.Windows. Forms.DataGrid dataGrid1;
private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;

private System.Componen tModel.Containe r components = null;

public Form1()
{
InitializeCompo nent();
CreateDataTable ();
}

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( );
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();

((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .BeginInit();
this.SuspendLay out();
//
// dataGrid1
//
this.dataGrid1. DataMember = "";
this.dataGrid1. HeaderForeColor =
System.Drawing. SystemColors.Co ntrolText;
this.dataGrid1. Name = "dataGrid1" ;
this.dataGrid1. Size = new System.Drawing. Size(296, 192);
this.dataGrid1. TabIndex = 0;
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(64, 208);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 1;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(160, 208);
this.button2.Na me = "button2";
this.button2.Ta bIndex = 2;
this.button2.Te xt = "button2";
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(296, 266);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {

this.button2,

this.button1,

this.dataGrid1} );
this.Name = "Form1";
this.Text = "Form1";
((System.Compon entModel.ISuppo rtInitialize)(t his.dataGrid1)) .EndInit();
this.ResumeLayo ut(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void CreateDataTable ()
{
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;
}

private void FillDataGrid()
{
//System.Diagnost ics.Debug.Asser t(!this.InvokeR equired,
"InvokeRequired ");
if (InvokeRequired )
{
BeginInvoke(new MethodInvoker(F illDataGrid));
return;
}
//dt.Rows.Clear() ; //2
dt.BeginLoadDat a();
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(mov eRow);
}
dt.EndLoadData( );
}

private void button1_Click(o bject sender, System.EventArg s e)
{
//dt.Rows.Clear() ; //0
FillDataGrid();
}

private void button2_Click(o bject sender, System.EventArg s e)
{
//dt.Rows.Clear() ; //1
Thread solveThread = new Thread(new ThreadStart(Fil lDataGrid));

solveThread.Nam e = "Solve";
solveThread.IsB ackground = true;
solveThread.Sta rt();
}
}
}
Mar 9 '06 #1
0 4839

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

Similar topics

5
2464
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'") temptable.Clear() For Each row As DataRow In temprows temptable.Rows.Add(row) Next
4
4318
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 Dim dt As New DataTable() Dim cnString As String Dim adapter As New OdbcDataAdapter() Dim qs1 As String = "Select * from dl" 'first part of query .... .... ....
1
1498
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 this: 'clear datatable datatbl.Rows.Clear() If PLCCount() <> 0 Then
6
1700
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
32261
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 datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable filtered by the dataview into a separate table? I only want to copy the rows from the main table that...
7
6601
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 include them in the set that is displayed to the user) but I don't want to delete the corresponding row from the database. I've tried using the .Rows.Remove() method but an exception is thrown to say I don't have a 'delete' command in the data...
5
5890
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 store a DataTable in any other way outside of our servers. In doing so, we leave ourselves open to large memory requirements. Furthermore, most web pages do not really support multiple changes per transaction. In other words, when the user submits...
1
6742
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 passing this dataset to function that expects a dataset with a datatable in it.
9
6974
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 population. Whenever I call the thread, I pass it a structure containing the table and a few other parameters. The table goes in as a reference, but the other items are passed normally.
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.