How to set DataGridViewComboBoxColumn to display nothing ?

Hi all,

  I would like to display nothing when the user chooses the first option of the combobox.

  In Visual Basic 6.0 I would use something like that :

  DataGridViewComboBoxColumn.SelectedIndex = -1

  I use the newest VS 2005 (no beta).

 Thanks !

 Best regards,

 Joao Araujo


Answer this question

How to set DataGridViewComboBoxColumn to display nothing ?

  • WhoCaresTheName

    One other way, the DataGridView supports the Ctrl+0 keystroke to enter a null value into a cell, so you can use that to clear out a combo box's value and set it back to null.

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"
    < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


  • Rod Wing

    I got it ! I inserted a new row into the DataTable object. Then the user can select an empty option that saves the NULL value into the database column.

    Yeahh!

    Best regards,
    Joao Araujo


    // create a new combobox column.
    DataGridViewComboBoxColumn dgvc = new DataGridViewComboBoxColumn();

    // set the column width
    dgvc.Width = Width;

    // set the database column which it is bound
    dgvc.DataPropertyName = FieldName;

    // set the column name
    dgvc.Name = FieldName;

    // set the header text
    dgvc.HeaderText = HeaderText;

    DataTable MyTable = CreateDataTable(sql, TableName, false);
    AddNullValueForDataTable(MyTable, DisplayMember, ValueMember);

    dgvc.DataSource = MyTable;
    dgvc.DisplayMember = DisplayMember;
    dgvc.ValueMember = ValueMember;

    // add the column into the grid
    dgrView.Columns.Add(dgvc);



            /// <summary>
            /// Adds null values into the DataTable. The value member is empty and
            /// the display member contains DBNull.Value.
            /// </summary>
            /// <param name="datatable">The source DataTable used to fill the combobox.</param>
            /// <param name="DisplayMember">String that specifies the property of the data source whose contents you want to display.</param>
            /// <param name="ValueMember">String that specifies the property of the data source from which to draw the value.</param>
            /// <returns>The adcional string to add to the select statement.</returns>
            public static void AddNullValueForDataTable(DataTable datatable
                , string DisplayMember
                , string ValueMember)
            {
                try
                {
                    // allows NULL values
                    datatable.Columns[ValueMember].AllowDBNull = true;
                    // create a new row
                    DataRow row = datatable.NewRow();
                    // set the values
                    row[DisplayMember] = " ";
                    row[ValueMember] = DBNull.Value;
                    // add to the DataTable
                    datatable.Rows.InsertAt(row, 0);
                }
                catch (Exception e)
                {
                    Sky4Main.MessageError(e);
                }
            }


     


  • Anindya Maiti

     Mark Rideout wrote:

    One other way, the DataGridView supports the Ctrl+0 keystroke to enter a null value into a cell, so you can use that to clear out a combo box's value and set it back to null.

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"
    < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />




    Thanks Mark ! It was very helpful for me, but for the 200 people that will use the program, I think this option is for them not so user friendly !

    Kind Regards !
    Joao Araujo


  • How to set DataGridViewComboBoxColumn to display nothing ?