Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Bug? DataGridView displays data entered in a TextBox before BindingSource.EndEdit() is invoked.
 

Bug? DataGridView displays data entered in a TextBox before BindingSource.EndEdit() is invoked.

As far as I understand data binding, when new data is typed into a control, other controls also bound to the same source do not immediately reflect the changes, but wait until EndEdit() is called.

In the simple setup described below the DataGridView violates this behaviour, immediately displaying the first new item of data typed into a TextBox.

Create a Win Forms App and place a DataGridView (dataGridView1), 4 TextBoxes (textBox1 to textBox4), a BindingSource (bindingSource1) and a Button (button1) on a form.

Add the following code.


Code Snippet

private void Form1_Load(object sender, EventArgs e)
{
button1.Text = "Add New";

dataGridView1.AllowUserToAddRows = false;

//create a table with 3 columns
DataTable dataTable = new DataTable();
dataTable.Columns.Add("A");
dataTable.Columns.Add("B");
dataTable.Columns.Add("C");


//bind grid to the binding source
dataGridView1.DataSource = bindingSource1;

//bind data to binding source
bindingSource1.DataSource = dataTable;

//bind text controls to same binding source
textBox1.DataBindings.Add("Text", bindingSource1, "A");
textBox2.DataBindings.Add("Text", bindingSource1, "B");
textBox3.DataBindings.Add("Text", bindingSource1, "C");

//bind this text control to same data as textBox1
textBox4.DataBindings.Add("Text", bindingSource1, "A");
}

private void button1_Click(object sender, EventArgs e)
{
bindingSource1.AddNew();
textBox1.Focus();
}



Run the program and click the Add New button. textBox1 gets focus. Now type in some text and press TAB to shift focus to textBox2. Notice how the text typed in is immediately shown in the blank row of the grid in the A column but is not shown in textBox4. Continue by typing some data into textBox2 and press TAB again. Focus shifts to textBox3. However, this time the grid doesn’t display the new item of data. Click Add New again and repeat the data entry. This time the grid doesn’t immediately display the data.

Run the program again, but in this test don’t use TAB to shift focus from textBox1 to textBox2. Type some data into textBox1 and shift focus to textBox2 by clicking on it. Notice how this time the data typed into textBox1 isn’t immediately shown in the grid.

What’s going on?

How can I stop the grid from doing this?

Can anyone shed any light on my problem?

Thanks,

Mort.


Mortimer Muggle  Tuesday, December 02, 2008 10:20 PM

Hi , Try following ,

Code Snippet

yourgrd.SuspendLayout();

//============== Do your work then ===============

yourgrd.ResumeLayout(false);

yourgrd.PerformLayout();

i hope this will help you
Pr.Wael  Wednesday, December 03, 2008 11:25 AM
Hi Pr.wa'el

Thank you for your suggestion. Unfortunately this doesn't fix the problem. The grid still behaves in the way I described in my first post.

Mort.




Mortimer Muggle  Wednesday, December 03, 2008 12:51 PM

The commit will be called when it is the Validated. The editing cell lost focus will fire the Validating event.

Myexp  Thursday, December 04, 2008 7:56 AM

A solution that comes to mind is to set the foreground colour to that of the background for the new row in the grid thus hiding the “premature�text.

Nevertheless, I would be grateful if someone could confirm the behaviour I have described, and whether this is a known issue with the DataGridView.

Thanks,

Mortimer.

Mortimer Muggle  Monday, December 08, 2008 7:49 PM

You can use google to search for other answers

Custom Search

More Threads

• Input string was not in a correct format GridView Row command
• Fire AddingNew Event of bindingsource without using AddNew method
• all solutions for concurrency violation
• Clear Cell DatagridView in Cell Validating event
• How to display a (bullet) Unicode character in a DataGridView
• Heirarchical data binding
• edit dataset with designer table property question
• ColumnHeadersDefaultCellStyle.Alignment
• How should I resize the datagrid
• DataGirdView: Vertical Scrollbar Problem