1

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.

Community
  • 1
  • 1
William
  • 21
  • 9
  • This isn't really clear. Do you want to make a cell or multiple cells in said columns into combo boxes? Or do you want to populate two comb boxes elsewhere on the form with what is in columns 2 and 3? – skwear Oct 13 '16 at 15:03
  • _How would I get the DGV columns 2 & 3 to display as a combo box_ You set them to be [DataGridViewComboBoxColumns](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn(v=vs.110).aspx) – stuartd Oct 13 '16 at 15:03
  • Skwear, I would like to make the cells in the said columns into combo boxes, be it the DGV has one row or 100, each row should display the combo box and set at whatever the value returned for that row. – William Oct 13 '16 at 15:10
  • Stuart, we don't use the wizard here as it would be probably be easier, so I have to do everything on the fly, don't ask why? :) – William Oct 13 '16 at 15:11
  • Stuart, after looking deeper at the link, this may work, will give it a try. Thank you. – William Oct 13 '16 at 15:24
  • Stuart, unfortunately this is not what I'm looking for, this removes columns from the DGV and then adds a combo box column(s), I'm looking to change the current/existing row of a cell to a combo box, be it 1 row or 100. Instead of displaying the code value 'P, T, I, etc...' for each row, it will be a combo box which will display the description for that value, 'Pending, Text, Interim, etc.'. – William Oct 13 '16 at 16:49

1 Answers1

0

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

William
  • 21
  • 9