【问题标题】:How to add ComboBox to WINFORM datagridview bound to datatable如何将 ComboBox 添加到绑定到数据表的 WINFORM datagridview
【发布时间】:2014-09-30 14:32:08
【问题描述】:

我是一名 SQL DBA,在 VS C# 和 Winforms 方面的技能水平较低。几天来,我一直在为向 DataGridView 列添加组合框而苦苦挣扎,但已经放弃了。我有一个数据表 dt1 和 datagridview dg1。 dg1.Datasource = dt1; dt1 是数据集 ds1 的成员。我正在提供来自数组的组合项。

我试过自动生成真假。

如果 autogeneration=true 我得到两个同名的列和 1 个组合框,它位于错误的列位置,我从 dt1 得到正确的数据

如果为 false 并且我以编程方式为 dg1 定义列,我不会从 dt1 获得任何数据。

我的代码应该是什么样的以及我缺少哪些可能的绑定或属性,因此我在第 4 列位置添加了一个“GRADE”组合框,并且 dg1 从 dt1 填充并从数组组合。

在阅读了数十篇博客并尝试了几天之后,完全感到沮丧。请帮忙。

    private DataGridViewComboBoxColumn CreateComboBox()
    {
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        {
            combo.Name = "comboColumn";
            combo.HeaderText = "Grade";
            ArrayList drl = new ArrayList();
            drl.Add("GS1");
            drl.Add("GS2");
            drl.Add("WG1");
            drl.Add("WG2");
            combo.Items.AddRange(drl.ToArray());
            combo.DataSource = drl;
            //combo.ValueMember = "EmployeeID";
            //combo.DisplayMember = "Grade";
            //combo.DataPropertyName = "Grade";
        }
        return combo;
    }

    public Employee()
    {
        InitializeComponent();
        WindowState = FormWindowState.Maximized;
        Ds1 = new DataSet("ds1");

        Dt1 = new DataTable("dt1");

        ds1.Tables.Add(dt1);

        dt1.Columns.Add("EmployeeID");
        dt1.Columns.Add("FirstName");
        dt1.Columns.Add("LastName");
        dt1.Columns.Add("Grade");
        dt1.Columns.Add("DOB");

        //initialize datagridview
        Dg1.AutoGenerateColumns = true;

        //dg1.Columns.Add("column4", " EmployeeID ");
        // dg1.Columns.Add("column4", " FirstName ");
        // dg1.Columns.Add("column4", " LastName ");
     Dg1.Columns.Add(CreateComboBox());
        // dg1.Columns.Add("column5", " DOB ");

        Dg1.DataSource = dt1;

    }

【问题讨论】:

    标签: c# winforms datagridview combobox


    【解决方案1】:

    已解决:经过几天的寻找和尝试,我尝试了一个我认为行不通的解决方案,因为它提到了一个未绑定的列,并且似乎需要 SQL 或其他一些连接使其成为绑定的列。事实证明没有必要绑定列。这就是你要做的。

    1.Open your Form designer an place Focus on your DataGridView and select properties.
    
    2.Scroll down to the Columns collection (mine was at the bottom under Misc.) and expand the collection.
    3.Add Column name and if binding to DataTable set the DataPropertyName to the dt column.  In my case I set both the same. 
    Also there is a drop down to choose the ColumnType
    4.Add your ComboBox column.  This has a few more settings.  You should look through all of them but I was interested in Items &
    HeaderText only.  I set HeaderText the same as ColumnName &
    DataPropertyName.  I then opened the Items and added my list.  
    There is also a DataSource. I presume that is for populating your
    ComboBox from a database, service, or sharepoint but I'm not doing
    that.
    5.That's it.
    

    在您的 .cs 代码文件中,您需要插入两行代码。我把我的放在表单的顶部,在那里我声明了我需要的所有方法都可以使用的数据表。

    <yourdatagridview>.AutoGenerateColumns = false;
    <yourdatagridview>.DataSource = <yourdatatable>;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多