Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to set DataGridViewComboBoxColumn to display nothing ?
 

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
Joccar  Wednesday, November 30, 2005 5:14 PM
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);
            }
        }


 
Joccar  Wednesday, November 30, 2005 6:52 PM

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"

Mark Rideout  Wednesday, November 30, 2005 7:18 PM
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);
            }
        }


 
Joccar  Wednesday, November 30, 2005 6:52 PM

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"

Mark Rideout  Wednesday, November 30, 2005 7:18 PM
 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"




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
Joccar  Thursday, December 01, 2005 11:01 AM

You can use google to search for other answers

Custom Search

More Threads

• combobox giving an error when field is null
• DataGridView ComboboxColumn error
• DataGridView not binding to implemented IList class?
• RowFilter issue?
• Unable to keep focus on a cell if cell validation fails in datagridview?
• Binding to DataRow / DataRowView problem
• DataGridView, how to capture a cell's KeyPress event...
• retrieving name of control from DataList after run
• DataSet - DataGridView - Refresh
• Inserting Data to Word 2002