Spooks
Spooks

Reputation: 7187

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.

Upvotes: 3

Views: 12944

Answers (2)

Denys Wessels
Denys Wessels

Reputation: 17049

    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);

Upvotes: 3

Dave Swersky
Dave Swersky

Reputation: 34810

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

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

Upvotes: 3

Related Questions