简体   繁体   中英

C# - Change existing column in a datagridview to a combobox

I populate a DGV with 4 columns (using a DataTable) on the fly and would like to make column 2 and 3 a combo box.

Data Returned values: ProjNum(1) = H-16-0001, StatusCode(2) = P, ActionCode(3) = C, and ActionSeqnum(4) = 0001

For status code and action code we have a look up tables: P = Pending C = Create

For each row I would like to have column 2 and 3 as a combo box and set itself to what the returned value is and the text 'Pending/Create'.

DataGridView1.DataSource = ClassName.GetAppData(); //Returns a datatable.

How would I get the DGV columns 2 & 3 to display as a combo box?

I have found a link and tried: DataGridView set column cell Combobox

and also have found the CellFormatting event but I can't seem to get it working.

I created functions for the look up tables which returns a data table but when trying to bind it using the methods above, I can't get it to work.

Coming from a PowerBuilder back ground, the .NET controls and functionality is new to me.

Thank you for your help.

Ok, after quite a bit of research and some assistance from more experienced C#.NET developers, it I have found something that works for me but it is time consuming.

In this case you need to create your controls before hand, I have 12 columns that are returned from my DS, which means 12 new controls. I have done only two below as an example, one DGV Text box and one DGV combobox.

private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView2.AutoGenerateColumns = false;

//
//Text Box column
        DataGridViewTextBoxColumn UserIDColumn = new DataGridViewTextBoxColumn();
        UserIDColumn.Name = "user_id";
        UserIDColumn.DataPropertyName = "user_id";
        UserIDColumn.HeaderText = "User ID";
        UserIDColumn.HeaderCell.Style.Font = new Font(dataGridView2.Font, FontStyle.Bold);
        UserIDColumn.ReadOnly = false;
        dataGridView2.Columns.Add(UserIDColumn);

//Adding a combobox
        DataGridViewComboBoxColumn ActionStatusCodeColumn = new DataGridViewComboBoxColumn();
        ActionStatusCodeColumn.Name = "StatusCode";
        ActionStatusCodeColumn.DataPropertyName = "StatusCode";
        ActionStatusCodeColumn.ReadOnly = false;
        ActionStatusCodeColumn.DataSource = GetStatusLkp();//This function returns a BindingSource which contains the noted members below StatusCode and StatusCodeDescrEng.
        ActionStatusCodeColumn.ValueMember = "StatusCode";
        ActionStatusCodeColumn.DisplayMember = "StatusCodeDescrEng";
        dataGridView2.Columns.Add(ActionStatusCodeColumn);

//Attach the Data property name to the columns
dataGridView2.Columns[0].DataPropertyName = "user_id";
dataGridView2.Columns[1].DataPropertyName = "StatusCode";

//Next bind your DGV
dataGridView2.DataSource = GetActionsData(); //This function returns a BindingSource which contains the noted members user_id and StatusCode.

}

It is a pain but it works.

If there is an easier way to do this, please let me know.

Thanks

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