Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set SelectedIndex in DataGridViewComboBoxColumn?

Tags:

c#

windows

i am using a datagridview in that i am using a datagridviewcomboboxcolumn, comboboxcolumn is displaying text but the problem is i want to select the first item of comboboxcolumn by default how can i do this

DataGridViewComboBoxColumn dgvcb = (DataGridViewComboBoxColumn)grvPackingList.Columns["PackingUnits"];
Globals.G_ProductUtility G_Utility = new Globals.G_ProductUtility();
G_Utility.addUnittoComboDGV(dgvcb);
DataSet _ds = iRawMaterialsRequest.SelectBMR(bmr_ID, branch_ID, "PACKING");
grvPackingList.DataSource = _ds.Tables[0];
int i = 0;
foreach (DataRow dgvr in _ds.Tables[0].Rows)
{
    grvPackingList.Rows[i].Cells["Units"].Value = dgvr["Units"].ToString();
    i++;
}
like image 948
Nighil Avatar asked Jan 28 '11 05:01

Nighil


4 Answers

The values available in the combobox can be accessed via items property

row.Cells[col.Name].Value = (row.Cells[col.Name] as DataGridViewComboBoxCell).Items[0];
like image 151
V4Vendetta Avatar answered Nov 09 '22 23:11

V4Vendetta


the best way to set the value of a datagridViewComboBoxCell is:

DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Value");
dt.Rows.Add("Item1", "0");
dt.Rows.Add("Item1", "1");
dt.Rows.Add("Item1", "2");
dt.Rows.Add("Item1", "3");
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
cmb.DefaultCellStyle.ForeColor = Color.BlueViolet;
cmb.FlatStyle = FlatStyle.Flat;
cmb.Name = "ComboColumnSample";
cmb.HeaderText = "ComboColumnSample";
cmb.DisplayMember = "Item";
cmb.ValueMember = "Value";
DatagridView dvg=new DataGridView();
dvg.Columns.Add(cmb);
cmb.DataSource = dt;
for (int i = 0; i < dvg.Rows.Count; i++)
{
dvg.Rows[i].Cells["ComboColumnSample"].Value = (cmb.Items[0] as 
DataRowView).Row[1].ToString();
}

It worked with me very well

like image 20
user1001875 Avatar answered Nov 10 '22 00:11

user1001875


If I had known about doing it in this event, it would have saved me days of digging and
trial and errors trying to get it to set to the correct index inside the CellEnter event.

Setting the index of the DataGridViewComboBox is the solution I have been looking for.....THANKS!!!

In reviewing all the issues other coders have been experiencing with trying to set
the index inside of a DataGridViewComboBoxCell and also after looking over your code,
all that anyone really needs is:
1. Establish the event method to be used for the "EditingControlShowing" event.
2. Define the method whereby it will:
    a. Cast the event control to a ComboBox.
    b. set the "SelectedIndex" to the value you want.
        In this example I simply set it to "0", but you'd probably want to apply so real life logic here.

Here's the code I used:

private void InitEvents()
{

    dgv4.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler( dgv4EditingControlShowing );

}


private void dgv4EditingControlShowing( object sender, DataGridViewEditingControlShowingEventArgs e )
{
   ComboBox ocmb = e.Control as ComboBox;
   if ( ocmb != null )
   {
      ocmb.SelectedIndex = 0;
   }
}
like image 22
MakStir Avatar answered Nov 10 '22 00:11

MakStir


If DataGridViewComboBoxCell already exist:

DataTable dt = new DataTable();
dt.Columns.Add("Item");
dt.Columns.Add("Value");
dt.Rows.Add("Item 1", "0");
dt.Rows.Add("Item 2", "1");
dt.Rows.Add("Item 3", "2");
dt.Rows.Add("Item 4", "3");

for (int i = 0; i < dvg.Rows.Count; i++)
{
    DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dvg.Rows[i].Cells[1];
    comboCell.DisplayMember = "Item";
    comboCell.ValueMember = "Value";
    comboCell.DataSource = dt;
};
like image 33
user2070217 Avatar answered Nov 10 '22 00:11

user2070217