Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...