How to display default text inside combo box inside datagridview in c# windows application. RRS feed

  • Question

  • Hi

    I am developing application in C#.

       In the design mode I have created combobox  'cmbItemName'

    inside datagridview "gvInvoiceDetails".

     I have added records using datatable inside combobox in form load event  .

    I want to display "select"    as    initial row  and it is default  inside combo box.  

    In the form load event,I have written code

    cmbItemName.Items.Insert(0, "--Select--");

                cmbItemName.DataSource = FethItemCode();

                cmbItemName.ValueMember = "ItemCode";

                cmbItemName.DisplayMember = "itemName";

      public DataTable FethItemCode()


                string sql = "Select Distinct ItemCode,itemName From ItemMaster";

                SqlConnection conn = new SqlConnection(conString);


                SqlDataAdapter da = new SqlDataAdapter(sql, conn);

                DataTable dt = new DataTable();


                return dt;



         to display "Select"  I have written code

           for (int i = 0; i < cmbItemName.Items.Count; i++)


                    if (cmbItemName.Items[i].ToString() == "0")//


                        cmbItemName.DefaultCellStyle.NullValue = "Select";



    But it is not working

    Is the code to be written in    gvInvoiceDetails_EditingControlShowing  event ?





    Saturday, July 5, 2014 8:35 AM


  • Yes, the combobox doesn't exist except when the cell is in edit mode, so therefore you cannot reference it until then. So you would need to use EditingControlShowing.

    But even better, I would just add --Select-- right into your query result set so you wouldn't have to deal with it.

    Change your query to:

    SELECT 0, '--Select--'
    SELECT DISTINCT ItemCode,itemName FROM ItemMaster;

    Bob - www.ContrivedExample.com

    • Proposed as answer by Carl Cai Wednesday, July 16, 2014 3:26 AM
    • Marked as answer by Barry Wang Thursday, July 17, 2014 2:51 AM
    Saturday, July 5, 2014 2:18 PM