binding, pulldata twice after EndEdit

  • Thread starter Thread starter Axel_H
  • Start date Start date
A

Axel_H

Guest
Hello,

i have a strange behavior with binding, sorting and row.EndEdit.

I have a DataTable connected to a Bindingsource and a Textbox which is binded to the BindingSource.

In the DataTable there is one Column (DataType is String) which is Sorted.

So then i add four rows, which contains the values "1" "2" "4" and "5".


With a Button_Click_Event, i add an new row, go to new position of the row

and set the Text-Member of the textbox to -> "3".

After that i call the Row.EndEdit() and what happened is:

The row is sorted to the proper position,

but the row number 2 with the value "2" is overwritten with "3", why????


The issue is shown in the following code (you can copy it inside an new project and start):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace BindingSourceExamples
{
public class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}

public Form1()
{
this.Load += new EventHandler(Form1_Load);
}

private DataTable table;
private BindingSource bs;
private TextBox textBox1;
private Button button1;
private Binding binding1;

void Form1_Load(object sender, EventArgs e)
{
// Create a new DataTable.
table = new DataTable();

// Create column.
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Column1";
column.AutoIncrement = false;
column.Caption = "FS1FGNR";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);

// Add Rows
DataRow row1 = table.NewRow();
row1[column.ColumnName] = "1";
table.Rows.Add(row1);

DataRow row2 = table.NewRow();
row2[column.ColumnName] = "2";
table.Rows.Add(row2);

DataRow row3 = table.NewRow();
row3[column.ColumnName] = "4";
table.Rows.Add(row3);

DataRow row4 = table.NewRow();
row4[column.ColumnName] = "5";
table.Rows.Add(row4);


// Add BindingSource
bs = new BindingSource();
bs.DataSource = table;

// Do Sorting
bs.Sort = column.ColumnName;

// Add Controls
textBox1 = new TextBox();
textBox1.Location = new Point(23, 70);
textBox1.Size = new Size(150, 20);

button1 = new Button();
button1.Text = "ADD";
button1.Click += new EventHandler(this.button1_Click);
button1.Location = new Point(23, 25);
button1.Size = new Size(100, 40);

this.ClientSize = new Size(292, 266);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);


// Add Binding
binding1 = new Binding("Text", bs, column.ColumnName, true, DataSourceUpdateMode.OnPropertyChanged);
textBox1.DataBindings.Add(binding1);


}


private void button1_Click(object sender, EventArgs e)
{
// Add New Row
DataRow newRow = table.NewRow();
table.Rows.Add(newRow);

// Set Position to new Row
int cnt = -1;
foreach (DataRowView row in ((DataView)bs.List))
{
cnt++;
if (Object.Equals(row.Row, newRow))
{
bs.Position = cnt;
break;
}
}

//Set Value
textBox1.Text = "3";

//EndEdit to do Sorting
((DataRowView)bs.List[0]).EndEdit();
}
}

I cannot explain this behavior, can u?

Is this a bug?


Thanks for help.

Continue reading...
 
Back
Top