datagridview automatically selects the first cell
-
Friday, June 04, 2010 11:08 PM
I am using the datagridview in an unbound mode. The select mode is set to 'CellSelect'. In this mode, the datagridview automatically selects the first visible cell. This is a problem when the datagridview is not the first control on the form, because when another control gets the initial focus, it appears to the user that there are now two controls with focus.
If you have more than one datagridview on the same form, the problem compounds itself since each datagridview will appear to have and active cursor.
I wonder if this behavior is a bug or done on purpose. If it is done on purpose, what was the reason for it? The only way I can find to work around the problem is to remove the cell from the collection of selected cells when the form is first displayed. I am not thrilled with this because it means a putting some 'kludgey' code in every single form that uses the datagridview (a lot in my case). Has anyone got a better work around?
Any thought are greatly appreciated.
Andy
All Replies
-
Monday, June 07, 2010 9:38 AMModerator
Hi andy,
This is by design as far as I know, but not very hard to find a workaround to your issue. You can follow the code snippet below:
public Form1() { InitializeComponent(); this.dataGridView1.HandleCreated += new EventHandler(dataGridView1_HandleCreated); } void dataGridView1_HandleCreated(object sender, EventArgs e) { DataGridView grid = sender as DataGridView; switch (grid.SelectionMode) { case DataGridViewSelectionMode.CellSelect: grid.SelectedCells[0].Selected = false;; break; case DataGridViewSelectionMode.FullRowSelect: grid.SelectedRows[0].Selected = false; break; default: //handle other cases. break; } }Let me know if this does not help.
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. -
Monday, June 07, 2010 5:30 PM
Hi Aland.
Thank you for the reply. When the data grid view is first created, it won't have any rows and therefore no cell is selected. My workaround was to de-select the first cell after filling the grid initially. It works but it represents another 'band aid' sprinkled through every form that has a grid. I didn't find a solution that I could implement in a custom control that inherited from the datagridview control.
Andy
-
Tuesday, June 08, 2010 6:11 AMModerator
Hi andy,
You said: I didn't find a solution that I could implement in a custom control that inherited from the datagridview control.
This is really an issue, it is boring to modify the code manually. I find another way to solve this issue which is more simpler and we do not need to modify the code of each DataGridView:public class MyDataGridView : DataGridView { protected override void OnSelectionChanged(EventArgs e) { base.OnSelectionChanged(e); if(this.Focused == false && this.SelectedCells.Count > 0) { this.ClearSelection(); } } protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); this.ClearSelection(); } }Let me know if this does not help.
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. -
Friday, June 11, 2010 3:19 AMModerator
Hi andy,
How is the issue now? Did my reply help?
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. -
Sunday, June 13, 2010 9:36 PM
Gawd I am beginning to really hate this data grid view control!! After experimenting with your suggested solution. I have found the problem to be a bit larger. The selectmode of the grid doesn't seem to matter much. When the grid is first instantiated, it has no cells and therefore no current cell. The first attempt to add a row to the cell using Rows.Add seems to be where the problem lies. When adding the first row, the .CurrentCell property is getting set. This seems to (in turn) cause a cell to be selected. Dealing with the cell selected problem does not fix the now larger problem where the .CurrentCell is set too. Rather, it only causes some wierd synchronization problems where the cell that is visible selected doesn't match the current cell.
Anyway, I think that if I focus on preventing the CurrentCell from being automatically selected when the first row is added to the grid, I will solve my problem. I haven't had any luck with that just yet.
Thanks!
Andy
-
Monday, June 14, 2010 3:55 AMModerator
Hi andy,
Sorry for not meet your needs. The solution in my last reply indeed has problems as you indicated. I did some research again and got a new solution.
Based on my understanding, there are two circumstances we need to clear the selection of the DataGridView:
1. When the Form is shown the first time.
Solution: We can override the OnShown method of the base form to clear the selection:private bool _isFirstShown = true; protected override void OnShown(EventArgs e) { base.OnShown(e); if (_isFirstShown) { UnselectDataGridView(this); _isFirstShown = false; } }2. After the data is bound.
Solution: We can override the OnDataBindingComplete method of the custom DataGridView to clear the selection:public class MyDataGridView1 : DataGridView { protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e) { base.OnDataBindingComplete(e); this.ClearSelection(); } }Let me know if this does not help.
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. -
Monday, June 14, 2010 4:43 AM
Hi Aland.
I am using this control in unbound mode. The problem clearly is that the currentcell is set by when the first row is added to the collection of rows. Not a good design at all. In bound mode *maybe* that makes sense (although I am not really sure). However, in unbound mode, the current cell should be set when the programmer sets it or when the user enters the grid control.
So what I am trying to do is prevent the current cell from getting set when the first row is added to the collection of rows. As you might expect, there is really important code that runs when the current cell changes. Having that code run before the grid has been setup is a problem. Setting up the grid means adding rows to it. A catch-22 I am afraid.
Andy
-
Monday, June 14, 2010 8:04 AMModerator
Hi andy,
Based on my understanding, try to avoid setting the current cell is impossible since this is by design and will affect most of other behaviors of DataGridView. One thing we need to keep in mind is that there must be an current cell in DataGridView. I tested the methods I mentioned in my last reply and both of them works fine. You can try it at first.
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. -
Monday, June 14, 2010 3:53 PM
Hi Aland.
I don't believe it is true to say there must be a current cell. When there are no rows in a grid, there is no current cell. After you add rows, you can set the current cell to nothing. I cannot find anyway to prevent the current cell from being set automatically when the first row is added to the grid. An unfortuneate design choice, but it is what it is. I will probably have to look to some third party control for a grid that is better suited for data entry. There are lots of problems using the data grid for this purpose (this problem, the navigation keys stop at read-only cells, the end edit for the last cell never gets fired, etc).
Andy
-
Friday, June 18, 2010 6:19 AMModerator
Hi andy,
I think I can say little on this issue. Since this is a feature of DataGridView and it is by design. The root issue is that you do not want to follow the idea of the designer of DataGridView to use it, but want it to have a different function.
In this case, I think the best way is to create a new control by yourself which can completely achieve what you expect. You can also search for other controls similar to DataGridView and can meet your needs.
A basic philosophy is if you want to use a component to do something so that you do not need to do the same thing from scratch, you need to follow the rules of the designer of that component, otherwise you need to give up it and create a new one by yourself on which you can do anything you want.
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.


