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 );
}
}
}
}