简体   繁体   English

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

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

I'm a SQL DBA with low skill level in VS C# and Winforms. 我是在VS C#和Winforms中技能水平较低的SQL DBA。 I've been struggling with adding a combo box to a DataGridView column for several days and have given up. 我一直在努力将组合框添加到DataGridView列中已有几​​天,并且已经放弃了。 I have a datatable dt1 and datagridview dg1. 我有一个数据表dt1和datagridview dg1。 dg1.Datasource = dt1; dg1.Datasource = dt1; dt1 is a member of dataset ds1. dt1是数据集ds1的成员。 I am providing combo items from an array. 我正在提供数组中的组合项目。

I have tried autogeneration true and false. 我尝试过自动生成是非。

If autogeneration=true I get two columns of the same name with 1 combo box and it's in the wrong column position and I get correct data from dt1 如果autogeneration = true,我将得到两个具有1个组合框的同名列,并且列位置错误,并且从dt1获得了正确的数据

If false and I programmatically define columns for dg1, I don't get any data from dt1. 如果为false并且我以编程方式为dg1定义了列,则不会从dt1获得任何数据。

What should my code look like and what possible bindings or properties am I missing so that I add a combo box for 'GRADE' in the 4th column position and dg1 populates from dt1 and combo from array. 我的代码应该是什么样子以及可能缺少的绑定或属性,因此我在第4列的位置添加了一个用于“ GRADE”的组合框,而dg1从dt1填充,而comb从数组填充。

Totally frustrated after reading dozens of blogs and trying things for several days. 在阅读了数十篇博客并尝试了几天后,完全感到沮丧。 Please help. 请帮忙。

    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;

    }

Resolved: After several days hunting and trying, I tried a solution I didn't think would work because it mentioned an unbound column and appeared to require a SQL or some other connection make it a bound column. 解决:经过几天的探索和尝试,我尝试了一个我认为不可行的解决方案,因为它提到了未绑定的列,并且似乎需要SQL或其他连接使其成为绑定列。 Turns out it isn't necessary to bind the columns. 原来没有必要绑定列。 Here is what you do. 这就是你要做的。

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.

In your .cs code file you need to insert two lines of code. 在您的.cs代码文件中,您需要插入两行代码。 I placed mine at the top of the form where I declare datatables I need available to all methods. 我在表格的顶部放置了我的表格,在该表格中声明了所有方法都需要的数据表。

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

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

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