|
-
Apr 22nd, 2008, 01:13 PM
#1
Thread Starter
Fanatic Member
[2008] Datagridview combobox column
I've added some items to the collection and it isn't bound to anything but now how do you set the default value? I know that it's .SelectedIndex when you drag and drop it from the toolbox.
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
-
Apr 22nd, 2008, 06:58 PM
#2
Re: [2008] Datagridview combobox column
The DataGridView works like this. You add columns and each column contains a cell in each row. The cells contain a drawn representation of a control, not an actual control. When you start editing the value in a cell, only then is a control created and displayed in the cell. If you then move to a different cell, the control is removed from the previous cell and displayed in the new cell, if that cell uses the same type of control for editing. Otherwise a new control of the appropriate type is created. The grid NEVER contains more than one control at a time, and it contains no controls at all if you're not editing. It's done this way because if every cell contains an actual control all the time your app would simply grind to a halt.
So, this means that there is no ComboBox control in any of the cells of your column when the grid is displayed. When you begin an editing session on a cell a control is created and embedded in the cell. At this point the cell's Value is assigned to the ComboBox's SelectedValue property. You can make as many changes within the ComboBox control as you like without affecting the cell hosting it. If you press Escape the control is removed without ever pushing data back down to the cell. If you move to a different cell the editing session ends and the ComboBox's SelectedValue is assigned to the cell's Value. If at the value is not valid, that's when you the grid throws a validation exception.
Now, what does this mean to you in the context of this question? It means that you have two choices:
1. Set the Value of a cell so that that value is pushed to the ComboBox when it is hosted in the cell.
2. Handle the EditingControlShowing event of the grid, which is raised when the control is embedded in the cell, and set the SelectedValue of the editing control.
-
Apr 23rd, 2008, 12:29 PM
#3
Thread Starter
Fanatic Member
Re: [2008] Datagridview combobox column
Actually I would like to know how to do both of your suggestions so I could know for future reference.
Using Visual Studio 2008
Please mark your thread RESOLVED if you no longer need help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|