473,722 Members | 2,295 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 3501

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

Similar topics

5
2961
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 code and run. There is nothing crazy about the code. I used the designer to add the dataset and to...
0
1977
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 coupled to xsd, xml and dataset I would have expected any discrepancies to have been well documented and...
3
2547
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 look like this :
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 update it to DataSet.
2
4827
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 tried capturing the textbox.text at datagrid's CurrentCellChanged event but by then the textbox.text...
12
1687
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
5672
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
1120
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 'DataSet12.Appl.Rows.InsertAt(dr, 0)' doesn't insert.
4
1275
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 database table but am not able to do so. Any help will be appreciated.
0
8863
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
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
9384
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...
1
9157
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8052
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6681
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
5995
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();...
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2147
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.