0

I have a DataGridView in my Windows Form with 3 forms. The first 2 columns are uneditable, and the last column should be editable. Only some cells in Column #3 are set to combo boxes (they are of type DataGridViewComboBoxCell). Because not all cells in Column #3 are combo boxes, is there a way to individually invoke this dropdown in these cells? Right now I can only see a way to do this on a whole column, but not all cells in column #3 are comboboxes. In my example below, if the content in column #2 says "Cell that requires combobox", then the cell in column #3 in that row should be a ComboBoxCell. Otherwise, it should just be an editable text cell. The code looks something like this:

Column 1 | Column 2 | Column 3

ex 1 | Cell that requires combobox | [editable textbox]

ex 2 | Cell that doesn't require combobox | [combobox dropdown]

DataGridView newdatagridview = new DataGridView();
var column1 = new DataGridViewTextBoxCell();
var column2 = new DataGridViewTextBoxCell();
var column3 = new DataGridViewComboBoxCell();
var row = new DataGridViewRow();
column1.Value = "ex1";
column2.Value = "ex2";
column3.Value = column2.Value;
row.Cells.Add(column1);
row.Cells.Add(column2);
if (column2.Value == "Cell that requires combobox") {
  // add options for the dropdown
  column3.Items.Add("0");
  column3.Items.Add("1");
  column3.Items.Add("2");
  column3.Items.Add("3");
  column3.Items.Add("4");
  //column3.ReadOnly = false; <- This didn't work
  row.Cells.Add(column3); //You only add a combocell if column2's value says "Cell that requires combobox"
}
newdatagridview.Rows.Add(row);
fibonaccilinguine
  • 101
  • 1
  • 2
  • 11
  • 1
    Does this answer your question? [Adding Different DataGridView Cell Types to a Column](https://stackoverflow.com/questions/18045248/adding-different-datagridview-cell-types-to-a-column) – Crowcoder Jun 23 '20 at 22:37
  • Okay I see you can't mix different types in a column. So does that mean column #3 should just be a comboboxcell, and depending on if it's supposed to be a comboboxcell or not I can change DisplayStyle to Nothing or something so that it behaves as a textbox? Or any better suggestion? – fibonaccilinguine Jun 23 '20 at 22:45
  • My suggestion is that maybe you re-think the design. If you are fighting convention that hard then maybe it is an indication that your approach is wrong. Maybe you want to implement a modal popup for editing records instead of doing it in a grid. You can much more easily control the design of the modal form. Just a thought. – Crowcoder Jun 23 '20 at 23:13

0 Answers0