Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have to bind a comboBox to a datagridview in a windows form application. i have used the below code.
C#
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();

cmb.HeaderText = "Installation";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 3;
cmb.Items.Add("--SELECT--");
cmb.Items.Add("Pre-Installation");
cmb.Items.Add("Installation");
cmb.Items.Add("Post-Installation");

By using the this code comboBox is binded in gridview, But it is displaying on the topside of the column, I need to display the comboBox to the center of the column and
--Select-- should display by default.

For displaying comboBox to center i have used this
C#
cmb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

Still its not working

Please Help.

Thanks in advance
Posted
Updated 1-Feb-15 23:30pm
v6
Comments
Sinisa Hajnal 2-Feb-15 5:56am    
Easy, this is not online real-time service.

Lets clarify the question: what is displayed when the grid is filled? You say --Select-- should be, if it is not, what is? Second, what do you mean it is displayed topside of the column? Is it row height greater then the combobox?

Try setting properties on cmb.ComboBox (this accesses internal control of the cell).
CHill60 2-Feb-15 7:32am    
Do you want this centred within the DGV but aligned with one column OR do you want the comboBox to be associated with a single cell (dynamically).
To get "--Select--" to display by default use cmb.SelectedIndex=0
Renjith_R 2-Feb-15 7:47am    
Thanks for reply,
in DGV DataGridViewComboBoxColumn does not contain a definition cmb.SelectedIndex=0
CHill60 2-Feb-15 8:09am    
Apologies - that only works with an actual combobox - however - are you actually trying to programmatically put a comboBox into a cell of the DGV?
Renjith_R 2-Feb-15 8:14am    
Yes,I am programmatically put comboBox in DGV,

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();

cmb.HeaderText = "Installation";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 3;
cmb.Items.Add("Pre-Installation");
cmb.Items.Add("Installation");
cmb.Items.Add("Post-Installation");
I need to display --Select-- as the default value .

Thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900