I had a listbox on my form which was databound to a bindingsource, but was running into the issue that listboxes seem to always start with an item selected, so I changed to a datagridview, but seem to be having the same issue. Is there anyway to surpress the selection of a row in a datagridview when you load the data? My work around right now is to not wire the SelectionChanged event until after the form load completes, but after I call ClearSelection in my form load. It seems like a kludge though. | | bpeikes Tuesday, August 25, 2009 1:41 PM | Hello,
Thanks for your feedback.
>What I'm trying to accomplish seems very straight forward. I want a listbox type of control, which I can load data into, and not have it automatically select the first item. It's hard to believe that there is no functionality for this.
When binding control to data component(e.g. DataTable), we actually use a help class,BindingManagerBase class, to manage the binding object. And the BindingManagerBase object has a property(Position), which is zero-based index that specifies a position in the underlying list. That why the first item is select when we reload or rebind the control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingmanagerbase.position.aspx
To avoid unnecessary calling of SelectedChanged event, weusually use a flag tojudge if we need call the event handler. However, if the code just needs to function when the position ofunderlying is changed, we can handle the PositionChanged event of the BindingManagerBaseobject.
DataTable dt = new DataTable(); private void Form6_Load(object sender, EventArgs e) { dt.Columns.Add("name"); dt.Columns.Add("price", typeof(decimal)); dt.Rows.Add("item1", 1.25); dt.Rows.Add("item2", 2.25); dt.Rows.Add("item3", 4.25); dt.AcceptChanges(); this.BindingContext[dt].PositionChanged+=new EventHandler(Form6_PositionChanged); this.listBox1.DisplayMember = "name"; this.listBox1.DataSource = dt; this.listBox1.SelectedIndex = -1; }
void Form6_PositionChanged(object sender, EventArgs e) { MessageBox.Show("Position changed!"); }
private void button1_Click(object sender, EventArgs e) { this.listBox1.DataSource = null; this.listBox1.DisplayMember = "name"; this.listBox1.DataSource = dt; this.listBox1.SelectedIndex = -1; }
Thanks, Rong-Chun Zhang MSDN Subscriber Support in Forum If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Edited byRong-Chun ZhangMSFT, ModeratorWednesday, August 26, 2009 8:07 AMAdding link
- Marked As Answer byRong-Chun ZhangMSFT, ModeratorMonday, September 07, 2009 8:17 AM
-
| | Rong-Chun Zhang Wednesday, August 26, 2009 8:06 AM | From what I have seen, the DataGridView always wants to have a selected item. I don't know of a work around to this. What are you trying to accomplish? Maybe there is a way to accomplish what you need without changing how the grid selection works? www.insteptech.com ;
msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! | | DeborahK Tuesday, August 25, 2009 2:57 PM | Actually you are wrong. The datagridview does not need to have a selected item. That's what ClearSelection does. As I said before, if I wire the SelectionChanged event after I call ClearSelection then there is no issue. I don't know if there will be issues when I reload the grid though. If there is, I will unwire the event, clear the selection, change the data and then rewire the event. The alternative will be to set a flag in the form that tells me if I want to handle the SelectionChanged event.
What I'm trying to accomplish seems very straight forward. I want a listbox type of control, which I can load data into, and not have it automatically select the first item. It's hard to believe that there is no functionality for this. | | bpeikes Tuesday, August 25, 2009 4:48 PM | Hello,
Thanks for your feedback.
>What I'm trying to accomplish seems very straight forward. I want a listbox type of control, which I can load data into, and not have it automatically select the first item. It's hard to believe that there is no functionality for this.
When binding control to data component(e.g. DataTable), we actually use a help class,BindingManagerBase class, to manage the binding object. And the BindingManagerBase object has a property(Position), which is zero-based index that specifies a position in the underlying list. That why the first item is select when we reload or rebind the control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingmanagerbase.position.aspx
To avoid unnecessary calling of SelectedChanged event, weusually use a flag tojudge if we need call the event handler. However, if the code just needs to function when the position ofunderlying is changed, we can handle the PositionChanged event of the BindingManagerBaseobject.
DataTable dt = new DataTable(); private void Form6_Load(object sender, EventArgs e) { dt.Columns.Add("name"); dt.Columns.Add("price", typeof(decimal)); dt.Rows.Add("item1", 1.25); dt.Rows.Add("item2", 2.25); dt.Rows.Add("item3", 4.25); dt.AcceptChanges(); this.BindingContext[dt].PositionChanged+=new EventHandler(Form6_PositionChanged); this.listBox1.DisplayMember = "name"; this.listBox1.DataSource = dt; this.listBox1.SelectedIndex = -1; }
void Form6_PositionChanged(object sender, EventArgs e) { MessageBox.Show("Position changed!"); }
private void button1_Click(object sender, EventArgs e) { this.listBox1.DataSource = null; this.listBox1.DisplayMember = "name"; this.listBox1.DataSource = dt; this.listBox1.SelectedIndex = -1; }
Thanks, Rong-Chun Zhang MSDN Subscriber Support in Forum If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Edited byRong-Chun ZhangMSFT, ModeratorWednesday, August 26, 2009 8:07 AMAdding link
- Marked As Answer byRong-Chun ZhangMSFT, ModeratorMonday, September 07, 2009 8:17 AM
-
| | Rong-Chun Zhang Wednesday, August 26, 2009 8:06 AM | Have you got any progress on this issue? If there is anything else we can help, welcome to post here.
If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Thanks, Rong-Chun Zhang
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. | | Rong-Chun Zhang Friday, September 04, 2009 9:06 AM |
|