473,549 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataSet - Errors editing rows after using InsertAt() - DataGrid Bo

Tried posting in the Winform Forum without much luck, so posting here...

After inserting a new data row to a DataTable that is bound to a
datagrid, I am unable to change data in a row that is after the newly
added row without getting bizarre results.

I have added the full code for the test below. Create a project drop
in the code and run. There is nothing crazy about the code. I used
the designer to add the dataset and to do the binding.

90% of the code is generated by dragging controls on the form.
From addButton_Click down is code that I manually typed, some
20 lines of code altogether.

To the best of my ability there is no possible way of providing a
demo that is smaller.
The barest way to describe the application and problem is I have
1) Dragged a DataGrid, DataSet, and 3 buttons onto the form
2) Added columns to the DataSet
3) Bound DataSet to DataGrid
4) Double clicked each button "Add Row" "Remove Binding" "Add Binding"
and added appropriate ~10 lines of code
5) Added default data to Form Load
6) Start App
7) Position cursor on row 1
8) Click "Add Row" Button
9) Modify row after new row and it BLOWS UP
Any help would be appreciated.

Cheers,
Dave
Test #1
=============== =======
1) Click on the row where ID = 1
2) Click "Add Row" button
3) Click on the cell ID = 2 Number = 4
4) Change the number from 4 to 5
5) Move off the row (i.e. hit the down arrow key)
6) Notice there are 2 rows with ID = 2 now
2, 5, 6
2, 4, 6
Test #2
=============== =======
1) Click on the row where ID = 1
2) Click "Add Row" button
3) Click "Remove Binding"
4) Click "Add Binding"
5) Click on the cell ID = 2 Number = 4
6) Change the number from 4 to 5
7) Move off the row (i.e. hit the down arrow key)
8) Notice there are 2 rows with ID = 2 now
2, 5, 6
2, 4, 6

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

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

namespace Controls
{
/// <summary>
/// Summary description for TestDataGrid.
/// </summary>
public class TestDataGrid : System.Windows. Forms.Form
{
private int idCounter = 100;

#region Unimportant Window stuff

private System.Windows. Forms.DataGrid theGrid;
private System.Windows. Forms.Button addButton;
private System.Data.Dat aSet myDataSet;
private System.Data.Dat aTable rateTable;
private System.Data.Dat aColumn dataColumn1;
private System.Data.Dat aColumn dataColumn2;
private System.Data.Dat aColumn dataColumn3;
private System.Windows. Forms.Button removeBinding;
private System.Windows. Forms.Button addBinding;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components =
null;

public TestDataGrid()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after
InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
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.theGrid = new
System.Windows. Forms.DataGrid( );
this.myDataSet = new System.Data.Dat aSet();
this.rateTable = new System.Data.Dat aTable();
this.dataColumn 1 = new
System.Data.Dat aColumn();
this.dataColumn 2 = new
System.Data.Dat aColumn();
this.dataColumn 3 = new
System.Data.Dat aColumn();
this.addButton = new
System.Windows. Forms.Button();
this.removeBind ing = new
System.Windows. Forms.Button();
this.addBinding = new
System.Windows. Forms.Button();

((System.Compon entModel.ISuppo rtInitialize)(t his.theGrid)).B eginInit();

((System.Compon entModel.ISuppo rtInitialize)(t his.myDataSet)) .BeginInit();

((System.Compon entModel.ISuppo rtInitialize)(t his.rateTable)) .BeginInit();
this.SuspendLay out();
//
// theGrid
//
this.theGrid.An chor =
((System.Window s.Forms.AnchorS tyles)((((Syste m.Windows.Forms .AnchorStyles.T op
| System.Windows. Forms.AnchorSty les.Bottom)
|
System.Windows. Forms.AnchorSty les.Left)
|
System.Windows. Forms.AnchorSty les.Right)));
this.theGrid.Da taMember = "RateTable" ;
this.theGrid.Da taSource = this.myDataSet;
this.theGrid.He aderForeColor =
System.Drawing. SystemColors.Co ntrolText;
this.theGrid.Lo cation = new
System.Drawing. Point(8, 40);
this.theGrid.Na me = "theGrid";
this.theGrid.Si ze = new
System.Drawing. Size(368, 184);
this.theGrid.Ta bIndex = 0;
//
// myDataSet
//
this.myDataSet. DataSetName = "NewDataSet ";
this.myDataSet. Locale = new
System.Globaliz ation.CultureIn fo("en-US");
this.myDataSet. Tables.AddRange (new
System.Data.Dat aTable[] {

this.rateTable} );
//
// rateTable
//
this.rateTable. Columns.AddRang e(new
System.Data.Dat aColumn[] {

this.dataColumn 1,

this.dataColumn 2,

this.dataColumn 3});
this.rateTable. TableName = "RateTable" ;
//
// dataColumn1
//
this.dataColumn 1.ColumnName = "ID";
this.dataColumn 1.DataType = typeof(int);
//
// dataColumn2
//
this.dataColumn 2.ColumnName = "Number";
this.dataColumn 2.DataType = typeof(int);
//
// dataColumn3
//
this.dataColumn 3.ColumnName = "Price";
this.dataColumn 3.DataType = typeof(int);
//
// addButton
//
this.addButton. Anchor =
((System.Window s.Forms.AnchorS tyles)((System. Windows.Forms.A nchorStyles.Bot tom
| System.Windows. Forms.AnchorSty les.Left)));
this.addButton. Location = new
System.Drawing. Point(16, 256);
this.addButton. Name = "addButton" ;
this.addButton. TabIndex = 1;
this.addButton. Text = "Add Row";
this.addButton. Click += new
System.EventHan dler(this.addBu tton_Click);
//
// removeBinding
//
this.removeBind ing.Anchor =
((System.Window s.Forms.AnchorS tyles)((System. Windows.Forms.A nchorStyles.Bot tom
| System.Windows. Forms.AnchorSty les.Left)));
this.removeBind ing.Location = new
System.Drawing. Point(136, 256);
this.removeBind ing.Name = "removeBinding" ;
this.removeBind ing.Size = new
System.Drawing. Size(104, 23);
this.removeBind ing.TabIndex = 3;
this.removeBind ing.Text = "Remove Binding";
this.removeBind ing.Click += new
System.EventHan dler(this.remov eBinding_Click) ;
//
// addBinding
//
this.addBinding .Anchor =
((System.Window s.Forms.AnchorS tyles)((System. Windows.Forms.A nchorStyles.Bot tom
| System.Windows. Forms.AnchorSty les.Left)));
this.addBinding .Location = new
System.Drawing. Point(288, 256);
this.addBinding .Name = "addBinding ";
this.addBinding .Size = new
System.Drawing. Size(104, 23);
this.addBinding .TabIndex = 4;
this.addBinding .Text = "Add Binding";
this.addBinding .Click += new
System.EventHan dler(this.addBi nding_Click);
//
// TestDataGrid
//
this.AutoScaleB aseSize = new
System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(400,
293);
this.Controls.A dd(this.addBind ing);
this.Controls.A dd(this.removeB inding);
this.Controls.A dd(this.addButt on);
this.Controls.A dd(this.theGrid );
this.Name = "TestDataGr id";
this.StartPosit ion =
System.Windows. Forms.FormStart Position.Center Screen;
this.Text = "TestDataGr id";
this.Load += new
System.EventHan dler(this.TestD ataGrid_Load);

((System.Compon entModel.ISuppo rtInitialize)(t his.theGrid)).E ndInit();

((System.Compon entModel.ISuppo rtInitialize)(t his.myDataSet)) .EndInit();

((System.Compon entModel.ISuppo rtInitialize)(t his.rateTable)) .EndInit();
this.ResumeLayo ut(false);

}
#endregion

#endregion

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addButton_Click (object sender,
System.EventArg s e)
{
Random random = new
Random(idCounte r);
DataRow row = rateTable.NewRo w();
int position = theGrid.Current RowIndex + 1;

row[0] = idCounter++;
row[1] = (int)( random.NextDoub le() * 100 );
row[2] = (int)( random.NextDoub le() * 10 );

rateTable.Rows. InsertAt( row, position );
rateTable.Accep tChanges();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void removeBinding_C lick(object sender,
System.EventArg s e)
{
this.theGrid.Da taSource = null;
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addBinding_Clic k(object sender,
System.EventArg s e)
{
this.theGrid.Da taMember = "RateTable" ;
this.theGrid.Da taSource = this.myDataSet;
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TestDataGrid_Lo ad(object sender,
System.EventArg s e)
{
int numRows = 5;
DataRow row;

for (int i = 0; i < numRows; i++)
{
row = rateTable.NewRo w();
row[0] = i * 1;
row[1] = i * 2;
row[2] = i * 3;

rateTable.Rows. Add( row );
}
}
}
}
Nov 17 '05 #1
0 3479

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

Similar topics

5
2939
by: Dave | last post by:
I tried posting this in the WinForm forum and got no hits so I am trying it here. After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is after the newly added row without getting bizarre results. I have added the full code for the test below. Create a project drop in the...
0
1964
by: Frosty | last post by:
Hi I am using the VS xsd designer to create a strongly typed dataset. The dataset is apparently successfully created, with no warnings or errors given. Is it not then to be expected that this dataset then should reflect all the details put into the xsd? Somewhere down the line there is not a 1:1 relationship. Be it that .NET is so tightly...
3
2531
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called "Resource" with the following structure : Resource{,en,fr,es} Yes.. these are the only languages supported actually. A couple of rows in that table would...
0
367
by: David | last post by:
Hello. I have some little problem with DataSet and I hope somebody will help me. So what's my problem: I have DataSet with some table in it lets say table name is "Suppliers", I have DataGrid to show the records from this DataSet and toolbar fornavigating and editing rows in DataSet. I'm using another form for editing data and the manualy...
2
4810
by: Alpha | last post by:
Hi, I have a window based program. One of the form has several textboxes and a datagrid. The textboxes are bind to the same dataset table as the datagrid and the text changes to reflect different row selected in the datagrid. I want to save the changes that user make in the textboxes when they select a different row in the datagrid. I...
12
1674
by: Bishoy George | last post by:
I have a dataset called ds1 filled with 2 tables Employees and Customers from Northwind database. I have dropdownList called ddLastName with the following properties: ddLastName.DataSource = ds1; ddLastName.DataMember = "Employees"; ddLastName.DataTextField = "LastName"; ddLastName.DataBind(); ddLastName.Items.Insert(0,"Select:");
10
5641
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1) DsStudentCourse1.AcceptChanges() i'm also wondering because w/ out AcceptChanges the data is still save into the database and it is now faster.
0
1112
by: paulhux174 | last post by:
Hi, I've a dataGrid set up (the standard msdn example) with a SqlDataAdapter, and DataSet. The problem is that after inserting a new row using the LinkButton1_Click() (code below) then edit the values I then try to update the values using "DataGrid1_UpdateCommand()" The FindByApplID does't find the row. It looks like the...
4
1271
by: Abby | last post by:
Hi all, I have a datagrid which i am using for updating the data to the database. It works fine for single row data however when i have multiple rows(records) associated with the same criteria(billno) it updates both the rows with the values for the row i updated. I thought of reading both the rows into a dataset and update that to the...
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7723
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. ...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7817
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...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5375
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...
0
3504
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...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.