简体   繁体   中英

How to add ComboBox to WINFORM datagridview bound to datatable

I'm a SQL DBA with low skill level in VS C# and Winforms. I've been struggling with adding a combo box to a DataGridView column for several days and have given up. I have a datatable dt1 and datagridview dg1. dg1.Datasource = dt1; dt1 is a member of dataset 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

If false and I programmatically define columns for dg1, I don't get any data from 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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