Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a specific Column Uneditable In datagridview?

Using a DataGridView, how can I make a specific column uneditable while the grid view itself has "Allow editing" enabled?

Also, how can I execute an event when the selected index in a ComboBox in the DataGridView has changed? Here, ComboBox is a column type.

Another question is, how can I make the header title aligned to the center? I can't find the appropriate property.

like image 306
Informatician Avatar asked Jul 01 '12 14:07

Informatician


1 Answers

You've got a few questions here.

(1) How can I make a specific column uneditable in DataGridView?

Set the ReadOnly flag on the particular column you want to make uneditable.

dataGridView.Columns["YourColumnName"].ReadOnly = true;

(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?

If it's in your DataGridView, it's not a ComboBox; it's a DataGridViewComboBoxColumn. According to MSDN:

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

This one I'm not familiar with, as I've never tried it myself. It appears you want to subscribe to the EditingControlShowing event and then see if something like this works for you (with a little tweaking).

(3) How can I make the header title align in the center?

Set the HeaderCell.Style.Alignment

dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
like image 181
Grant Winney Avatar answered Oct 19 '22 21:10

Grant Winney