繁体   English   中英

如何将ComboBox添加到绑定到datatable的WINFORM datagridview

[英]How to add ComboBox to WINFORM datagridview bound to datatable

我是在VS C#和Winforms中技能水平较低的SQL DBA。 我一直在努力将组合框添加到DataGridView列中已有几​​天,并且已经放弃了。 我有一个数据表dt1和datagridview dg1。 dg1.Datasource = dt1; dt1是数据集ds1的成员。 我正在提供数组中的组合项目。

我尝试过自动生成是非。

如果autogeneration = true,我将得到两个具有1个组合框的同名列,并且列位置错误,并且从dt1获得了正确的数据

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

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

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

    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;

    }

解决:经过几天的探索和尝试,我尝试了一个我认为不可行的解决方案,因为它提到了未绑定的列,并且似乎需要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>;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM