Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a comboBox to a gridview in WinForms

I am trying to create a gridview with a string column, a checkbox column, and a dropdownlist/combobox column. The first two are finished (all code behind), just need help with the last one.

DataTable dt = new DataTable("tblAir");
            dt.Columns.Add("Flight Details", typeof(string));
            dt.Columns.Add("Prefered Seating", typeof(bool));
            //doesn't work 
            dt.Columns.Add("Add Remark", typeof(ComboBox));

The data for the combobox is being supplied on load as we cannot work with a database.

like image 842
Spooks Avatar asked Nov 26 '25 17:11

Spooks


2 Answers

Peter Bromberg has a detailed article on creating a Winforms gridview with comboboxes:

http://www.eggheadcafe.com/articles/20060202.asp

like image 76
Dave Swersky Avatar answered Nov 28 '25 13:11

Dave Swersky


    DataAccessLayer dal = new DataAccessLayer();
    DataTable movies = dal.GetMovies();

    gvMovies.DataSource = movies;
    gvMovies.AllowUserToAddRows = false;
    gvMovies.AllowUserToDeleteRows = false;

    //Create the new combobox column and set it's DataSource to a DataTable
    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    col.DataSource = dal.GetMovieTypes(); ; 
    col.ValueMember = "MovieTypeID";
    col.DisplayMember = "MovieType";
    col.DataPropertyName = "MovieTypeID";

    //Add your new combobox column to the gridview
    gvMovies.Columns.Add(col);
like image 23
Denys Wessels Avatar answered Nov 28 '25 12:11

Denys Wessels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!