Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView BindingSource Change Position
 

DataGridView BindingSource Change Position

Hello:

For my UI, I have a ComboBox and a DataGridView.  The user will select a Person from the ComboBox, and the DataGridView will display the Person.Schools.  Person.Schools is defined as a List<string>.

Here's the code...

BindingSource bs = new BindingSource();
bs.DataSource = listPersons;

this.comboBox1.DataSource = bs;
this.dataGridView1.DataBindings.Add("DataSource", bs, "Schools");

Well, I want the user to interface with the DataGridView and modify the school if needed.  This is built-in to the DataGridView; the user can begin typing in the DataGridViewCell.  When the user hits the Enter button, the focus will move to the next school, which is located below the previous cell the person is editing.

This is the behavior I want.  However, I want to add one last behavior.  When the user gets to the last school in the list, which is the last row in the DataGridView, and the user hits enter, I want to move to the next Person in the BindingList.

I've tried the following in the CellFormatting...

if (this.dataGridView1.CurrentCell.RowIndex == this.dataGridView1.Rows.Count - 1)
{
  ((BindingSource)this.comboBox1.DataSource).Position++;  // Don't worry about checking for going past the BindingSource.Count
}
This works, but when it the BindingSource increments its position, the NEXT Person's first school is also changing.  EXA:  If I have two Persons, Person1 is currently selected.  He has two schools.  I change the first school for him to "U. of ABC."  I hit enter, the UI moves the focus to the next cell.  I change this cell to "U. of DEF."  I hit enter, well, the CellFormatting event fires and increments the position of the BindingSource.  After the position is incremented, I can see on the UI Person2 is in the ComboBox, and the FIRST school in the DataGridView -- although I didn't set it -- is "U of DEF."

How can I change the position of the BindingSource, moving it to the next Person in the list, without changing the first school of the next Person?


Thank you,


Trecius
Trecius  Friday, October 02, 2009 1:29 AM
Hi Trecius,

You can call the CancelEdit method of the DataGridView to cancel edit mode and ignore changes. The below document shows the details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.canceledit.aspx

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 5:05 AM
If I understood correctly, the problem is that the update intended for the last school of the previous person is submitted as if it were for the first school of the next person.  If so, it sounds like you are using the incorrect DataGridView event to move the position of the binding source.  Basically, you are moving it too early in the process.  I don't have a specific event in mind, but it shouldn't be too hard to find out which one yields the correct behavior.

Also, I would have advanced the position in the CurrencyManager object, not the binding source, but that's just me.

((CurrencyManager)this.BindingContext[comboBox1.DataSource]).Position++;

I really don't know if that would make a difference or not.
MCP
webJose  Friday, October 02, 2009 1:47 AM
Hi Trecius,

You can call the CancelEdit method of the DataGridView to cancel edit mode and ignore changes. The below document shows the details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.canceledit.aspx

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, October 07, 2009 5:05 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagrid filter row selected.
• autoincrement seed set to -1 updates db to this value
• Adding default cell values
• Data Problem / Committal
• Duplicate items on toolstripComboBox
• question regarding tabs
• DataGridView (VB.net) problems, nonvisible rows are still visible or the DataGridView is empty despite having a DataSource
• Retrieve Data From DataGridViewComboBoxColumn And Display In TextBox
• BindingSource and dirty bits. How to do this?
• myGridColumn.NullText Help