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.