Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change the style of DataGridViewComboBoxCell from Drop-Down to "Simple"
please help me i really need it.

here is my Code



C#
using System;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace DataGridViewComboBoxes
    {
 
    // *************************************************** class Form1

    public partial class Form1 : Form
        {
 
        // *************************************** enums and constants

        enum ControlTypes
            {
            COMBO,
            TEXT
            }
 
        const int                   CITIES_ROW = 1;
        const int                   CITIES_CELL = 1;
        const int                   COUNTRIES_ROW = 0;
        const int                   COUNTRIES_CELL = 1;
 
        const int                   NUMBER_COLUMNS = 2;
        const int                   NUMBER_ROWS = 2;
 
        // ************************************************* variables

        List < string >             Australian_cities = 
                                        new List < string > ( ) { 
                                            "Sydney", 
                                            "Melbourne", 
                                            "Brisbane", 
                                            "Perth", 
                                            "Adelaide" };
        DataGridViewComboBoxCell    countries_cell;
        DataGridViewComboBoxCell    cities_cell;
        List < string >             cities;
        List < string >             Indian_cities = 
                                        new List < string > ( ) { 
                                            "Mumbai", 
                                            "Delhi", 
                                            "Bangalore", 
                                            "Karnataka", 
                                            "Hyderabad" };
        List < string >             Pakistani_cities = 
                                        new List < string > ( ) { 
                                            "Karachi", 
                                            "Lahore", 
                                            "Faisalabad", 
                                            "Rawalpindi", 
                                            "Punjab" };
         List < string >             USA_cities = 
                                        new List < string > ( ) { 
                                            "New York", 
                                            "Los Angeles", 
                                            "Chicago", 
                                            "Houston", 
                                            "Philadelphia" };
        List < string >             countries = 
                                        new List < string > ( ) { 
                                            "IND", 
                                            "PAK", 
                                            "AUS", 
                                            "USA" };
 
        // ***************************************************** Form1

        public Form1 ( )
            {
            InitializeComponent ( );
 
            initialize_datagridview ( NUMBER_ROWS, NUMBER_COLUMNS );
            load_sample_data ( );
 
            dataGridView1.DataError += 
                new DataGridViewDataErrorEventHandler ( 
                    dataGridView1_DataError );
            }
 
        // *********************************** dataGridView1_DataError

        void dataGridView1_DataError ( 
                                object                         sender, 
                                DataGridViewDataErrorEventArgs e )
            {
 
            }
 
        // *********************************** initialize_datagridview

        void initialize_datagridview ( int rows, 
                                       int columns )
            {
 
            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
 
            for ( int i = 0; ( i < columns ); i++ )
                {
                string i_plus_one = ( i + 1 ).ToString ( );
 
                dataGridView1.Columns.Add ( "Col" + i_plus_one, 
                                            "Stage" + i_plus_one );
                }
 
            dataGridView1.Rows.Add ( rows );
                                        // make first row columns into 
                                        // DataGridViewComboBoxCell
            dataGridView1.Rows [ 0 ].Cells [ 1 ] = 
                                    new DataGridViewComboBoxCell ( );
            dataGridView1.Rows [ 1 ].Cells [ 1 ] = 
                                    new DataGridViewComboBoxCell ( );
 
            dataGridView1.Rows [ 0 ].Cells [ 0 ].Value = "Country";
            dataGridView1.Rows [ 1 ].Cells [ 0 ].Value = "City";
 
            dataGridView1.EditingControlShowing +=
                new DataGridViewEditingControlShowingEventHandler (
                    dataGridView1_EditingControlShowing );
            }
 
        // ****************************************** load_sample_data

        private void load_sample_data ( )
            {
 
            countries_cell = ( DataGridViewComboBoxCell ) 
                                dataGridView1.Rows [ COUNTRIES_ROW ].
                                              Cells [ COUNTRIES_CELL ];
            cities_cell = ( DataGridViewComboBoxCell ) 
                                dataGridView1.Rows [ CITIES_ROW ].
                                              Cells [ CITIES_CELL ];
            countries_cell.DataSource = null;
            countries_cell.DataSource = countries;
            }
 
        // *********************** dataGridView1_EditingControlShowing

        void dataGridView1_EditingControlShowing ( 
                    object                                     sender, 
                    DataGridViewEditingControlShowingEventArgs e )
            {
            ControlTypes    control_type;
            Object          editing_control;
 
            try
                {
                editing_control = 
                    ( DataGridViewTextBoxEditingControl ) e.Control;
                control_type = ControlTypes.TEXT;
                }
            catch
                {
                editing_control = 
                    ( DataGridViewComboBoxEditingControl ) e.Control;
                control_type = ControlTypes.COMBO;
                }
 
            if ( control_type == ControlTypes.COMBO )
                {
                ComboBox  combo_box = ( ComboBox ) editing_control;
 
                if ( combo_box != null )
                    {
                    combo_box.TextChanged -= 
                        new EventHandler ( 
                            editingcontrol_TextChanged );
                    combo_box.TextChanged += 
                        new EventHandler ( 
                            editingcontrol_TextChanged );
                    }
                }
            }
 
        // ******************************** editingcontrol_TextChanged

        void editingcontrol_TextChanged ( object    sender, 
                                             EventArgs e )
            {
 
            if ( countries_cell.Selected )
                {
                ComboBox    combo_box = ( ComboBox ) sender;
                string      text = combo_box.Text;
 
                if ( !String.IsNullOrEmpty ( text ) )
                    {
                    switch ( text )
                        {
                        case "IND":
                            cities = Indian_cities;
                            break;
 
                        case "PAK":
                            cities = Pakistani_cities;
                            break;
 
                        case "AUS":
                            cities = Australian_cities;
                            break;
 
                        case "USA":
                            cities = USA_cities;
                            break;
 
                        default:
                            throw new ApplicationException ( 
                                "Unrecognized country" );
                        }
                                        // avoid DataError by rebuild
                                        // of cities_cell
                    cities_cell.Dispose ( );
                    cities_cell = ( DataGridViewComboBoxCell ) 
                                        dataGridView1.
                                            Rows [ CITIES_ROW ].
                                            Cells [ CITIES_CELL ];
                    combo_box.TextChanged -= 
                        new EventHandler ( 
                            editingcontrol_TextChanged );
                    combo_box.TextChanged += 
                        new EventHandler ( 
                            editingcontrol_TextChanged );
 
                    cities_cell.DataSource = null;
                    cities_cell.DataSource = cities;
                    }
                }
            }
 
        } // class Form1

    } // namespace DataGridViewComboBoxes
Posted
Updated 13-Jun-14 6:40am
v2

1 solution

C#
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is ComboBox)
    {
        ComboBox box = (ComboBox)e.Control;
        box.DropDownStyle = ComboBoxStyle.Simple;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900