【问题标题】:Adding a comboBox to a gridview in WinForms将组合框添加到 WinForms 中的网格视图
【发布时间】:2010-08-24 13:24:05
【问题描述】:

我正在尝试创建一个带有字符串列、复选框列和下拉列表/组合框列的网格视图。前两个已经完成(所有代码都在后面),只需要最后一个的帮助。

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

组合框的数据在加载时提供,因为我们无法使用数据库。

【问题讨论】:

    标签: c# winforms gridview


    【解决方案1】:

    Peter Bromberg 有一篇关于使用组合框创建 Winforms 网格视图的详细文章:

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

    【讨论】:

    • 在试图弄清楚时遇到了一些麻烦,我有 DataGridComboBoxColumn 类,不知道如何填充组合框或如何将其添加到我的 dt.Columns...- 基本上我需要填充DataTable 与我的选择,不确定如何做到这一点,因为它解释了它只是与数据库一起使用
    【解决方案2】:
        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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多