views:

5784

answers:

4

I have a datagridview with a DataGridViewComboboxColumn column with 3 values:

"Small", "Medium", "Large"

I get back the users default which in this case is "Medium"

I want to show a dropdown cell in the datagridview but default the value to "Medium". i would do this in a regular combobox by doing selected index or just stting the Text property of a combo box.

A: 

Are you retrieving the user data and attempting to set values in the DataGridView manually, or have you actually bound the DataGridVew to a data source? Because if you've bound the grid to a data source, you should just need to set the DataPropertyName on the column to be the string name of the object Property:

[DataGridViewComboboxColumnName].DataPropertyName = "PropertyNameToBindTo";

Or do you mean you want it to default to Medium for a new row?

I am adding the values programatically using items.AddRange
ooo
+1  A: 

When you get into the datagridview it is probably best to get into databinding. This will take care of all of the selected index stuff you are talking about.

However, if you want to get in there by yourself,

DataGridView.Rows[rowindex].Cells[columnindex].Value

will let you get and set the value associated to the DataGridViewComboBoxColumn. Just make sure you supply the correct rowindex and columnindex along with setting the value to the correct type (the same type as the ValueMember property of the DataGridViewComboBoxColumn).

codefoo
This works but only reflects after I move out of the said Cell of which value is being set.
Ismail
A: 

i wan to set the index to DataGridViewComboBoxColumn control

Dagar
A: 

Do accomplish this task you should do something like this:-

          this.dataGridViewStudentInformation.Columns[ColumnIndex].DataPropertyName = dataGridViewStudentInformation.Columns[2].Name ; //Set the ColumnName to which you want to bind.  

And set the default value in Database as Medium.

Sandeep