[英]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. 我正在尝试使用字符串列,复选框列和下拉列表/组合框列创建gridview。 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. 组合框的数据是在加载时提供的,因为我们无法使用数据库。
Peter Bromberg has a detailed article on creating a Winforms gridview with comboboxes: Peter Bromberg有一篇关于使用组合框创建Winforms gridview的详细文章:
http://www.eggheadcafe.com/articles/20060202.asp http://www.eggheadcafe.com/articles/20060202.asp
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.