locked
DataGridViewComboBoxColumn set default value? RRS feed

  • Question

  • Hai, i got a datagridview and i need to add a DataGridViewComboBoxColumn by programmatically.
    Below is my code :

                        DataGridViewComboBoxColumn objClearance = new DataGridViewComboBoxColumn();
                        objClearance.Items.Add("0");
                        objClearance.Items.Add("1");
                        objClearance.Items.Add("2");
                        objClearance.Items.Add("3");

    In normal combobox i use comboboxA.selectedindex = 0; to set its default value.
    But how i going to set the default item for DataGridViewComboBoxColumn?

    Thanks!
    Friday, April 3, 2009 7:58 AM

Answers

  • Hi kklow.27,

    Have you tried the following sample code:

            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 0) // your DataGridViewComboBoxColumn index 
                {
                    e.Value = "Default value";
                }
            }

    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by kklow.27 Wednesday, April 8, 2009 7:00 AM
    Sunday, April 5, 2009 2:22 PM

All replies

  • Hi kklow.27,

    Have you tried the following sample code:

            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 0) // your DataGridViewComboBoxColumn index 
                {
                    e.Value = "Default value";
                }
            }

    Best regards,
    Guo
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by kklow.27 Wednesday, April 8, 2009 7:00 AM
    Sunday, April 5, 2009 2:22 PM
  •         private void dgvVisita_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.ColumnIndex == 5)
                {
                    e.Value = "0";
                }
            }

    I used this but everytime I select a number from the ComboBox, It returns to "0".

    Wednesday, January 11, 2012 8:08 PM