none
datagridviewcomboboxcolumn select the first item

    Question

  • Hi all,
    I'm using a Datagridviewcomboboxcolumn in to dsiplay some data in my datagridview. After binding the data to the datagridviewcombobox how can i make it display the first item in the box?
    My code is as follow
    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
                DataTable dt = getData();//get data from database
                col.DataSource = dt;
                col.DisplayMember = "LastName";
                if (col.Items.Count > 0)//There is at least one item in the Combobox
                {
                    //Select the first item
                }
                dataGridView1.Columns.Add(col);
    Tuesday, January 08, 2008 12:19 AM

Answers

  • HI

     

    here i have modified the code.  

     

     

    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
                DataTable dt = getData();//get data from database
                col.DataSource = dt;
                col.DisplayMember = "LastName";
                if (col.Items.Count > 0)//There is at least one item in the Combobox
                {
                    //Select the first item
                }
                dataGridView1.Columns.Add(col);

    after binding the data  you can write this code

     

    DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dataGridView1.Columns["comboColumn"];

    for (int X = 0; X <= dataGridView1.Rows.Count - 1; X++)

    {

    DataGridViewCell cell = dataGridView1.Rows[X].Cells["comboColumn"];

     

    if (col.Items.Count > 0)

    {

    cell.Value = col.Items[0].ToString();

    }

    }

    Wednesday, January 09, 2008 11:52 AM

All replies

  • HI

     

    here i have modified the code.  

     

     

    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
                DataTable dt = getData();//get data from database
                col.DataSource = dt;
                col.DisplayMember = "LastName";
                if (col.Items.Count > 0)//There is at least one item in the Combobox
                {
                    //Select the first item
                }
                dataGridView1.Columns.Add(col);

    after binding the data  you can write this code

     

    DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dataGridView1.Columns["comboColumn"];

    for (int X = 0; X <= dataGridView1.Rows.Count - 1; X++)

    {

    DataGridViewCell cell = dataGridView1.Rows[X].Cells["comboColumn"];

     

    if (col.Items.Count > 0)

    {

    cell.Value = col.Items[0].ToString();

    }

    }

    Wednesday, January 09, 2008 11:52 AM
  • Hiii.

    I am using DataGridViewComboBoxColumn and on loading data from Database value will be show as empty... It doesn't work.... 

     

    Please help me.....

     

    Thanks

    Wednesday, November 23, 2011 6:53 PM