hi there, i have a datagrid view in C#. i got the data to be displyed from the database to the datagridview combo column, as shown below:

DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvAction.Rows[0].Cells[1].OwningColumn;
                comboBox.Items.Clear();
                comboBox.Items.AddRange(m.LoadUSEmp());

this code is loaded when the form loads but when it load i want to display one specific value from the data that is being displayed, (the users name should be displayed). i have method to get the name of the users name. how can i display the users name first in the combo box.

thanxxxx

I didn't completely figure it out but did you try this?

comboBox1.SelectedIndex = comboBox1.FindString("UserName");

I didn't completely figure it out but did you try this?

comboBox1.SelectedIndex = comboBox1.FindString("UserName");

heyvwhen i try with the code i have posted below

1.
      DataGridViewComboBoxColumn comboBox = (DataGridViewComboBoxColumn)dgvAction.Rows[0].Cells[1].OwningColumn;
   2.
      comboBox.Items.Clear();
   3.
      comboBox.Items.AddRange(m.LoadUSEmp());

SelectedIndex is not an option in the combobox.

thanxxxxx

In the help on DataGridViewComboBoxColumn

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx

It states:

You can populate the cells manually by setting their Value properties. Alternatively, you can bind the column to the data source indicated by the DataGridView..::.DataSource property. If the DataGridView is bound to a database table, set the column DataPropertyName property to the name of a column in the table. If the DataGridView is bound to a collection of objects, set the DataPropertyName property to the name of an object property.

You can populate the column drop-down list manually by adding values to the Items collection. Alternatively, you can bind the drop-down list to its own data source by setting the column DataSource property. If the values are objects in a collection or records in a database table, you must also set the DisplayMember and ValueMember properties. The DisplayMember property indicates which object property or database column provides the values that are displayed in the drop-down list. The ValueMember property indicates which object property or database column is used to set the cell Value property.

and later

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.