0

I am reading an xml file using dataset and then i am creating a datagridview and assigning the table from dataset to datagridview.datasource dynamically. The problem i am facing here is, i want to add a combobox for one cell in datagridview.

Below is my code :

datagridview1.AllowUserToAddRows = false;
datagridview1.AllowUserToDeleteRows = false;
datagridview1.RowHeadersVisible = false;
datagridview1.AutoSizeColumnsMode =   DataGridViewAutoSizeColumnsMode.AllCells;
datagridview1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
datagridview1.DataMember = "";
datagridview1.DataSource = my_dataTable;
datagridview1.Columns["first name"].ReadOnly = true;
datagridview1.Columns["Second name"].Visible = false;
datagridview1.Columns["place"].Visible = false;
datagridview1.Columns["address"].Visible = false;
string[] datasource = { "add1", "add2" };
DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell();
combo.DataSource = datasource;
datagridview1.Rows[2].Cells[2] = combo; 

It is giving me datagridviewcomboboxcell value is not valid error.If i give some value then it runs well but not able to see the combobox in datagridview.

Nihal Kumar
  • 1
  • 1
  • 5
  • If i am adding items to combo, it is throwing an exception with error "Items collection cannot be modified when the DataSource property is set".I think adding item is same as the datasource.Please correct me if my understanding is incorrect. – Nihal Kumar Jun 05 '15 at 12:54

1 Answers1

0

You can use the DataSource to feed to Items, but using a simple string array will not work.

Instead you can convert the array to a List<string> :

string[] datasource = { "add1", "add2" };
DataGridViewComboBoxCell combo = new DataGridViewComboBoxCell();
combo.DataSource = datasource.ToList();
DGV.Rows[2].Cells[2] = combo; 

To set a value you can use..

combo.Value = combo.Items[0];

..after having set the cell.

For better control, especially for having spearate Display- and ValueMembers you can switch to a Dictionary or use a List<someClass>..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Throwing an exception with error "InvalidArgument=Value of '0' is not valid for 'index'". – Nihal Kumar Jun 05 '15 at 13:48
  • Make sure you set the value only __after__ setting the cell! – TaW Jun 05 '15 at 13:54
  • The error got resolved and whatever value i am setting is being shown properly but combobox is not visible. Instead the item being shown in text box. i.e the text box(cell) didn't get replaced with combobox. – Nihal Kumar Jun 08 '15 at 06:54
  • That sounds weird. Here the combobox shows just as expected. The only explanation would be that you are doing something else afterwards that reverts the change, like maybe, esp. if the DGV has autocreated columns on, when you do fresh databinding the th DGV..? – TaW Jun 08 '15 at 07:19
  • I am adding few rows to that DGV after but now i moved it up. So now i am adding combobox at the last but still it only first item is shown and that too in text box not in combobox :( – Nihal Kumar Jun 08 '15 at 07:41
  • Hm. _I am adding few rows_ So the DGV is not data-bound? _I moved it up_ I would not have expected that to help here; it is all about columns and cells, not so much rows; (unless you remove them, that is..). Try this simple test: add a button and in its click event copy the code that creates the cobobox! See if you can click it and if it works.. ? – TaW Jun 08 '15 at 07:47
  • I copied the code in button click event handler. So whenever i am clicking the button it throws an exception "System.ArgumentException : DataGridViewComboboxCell value is not valid. To replace this default dialog please handle the DataError event".After cancelling the error dialog the combobox is appearing. i handled DataGridViewDataErrorEventHandler event then this error went away but still i didn't get what i want. How can i handle it without clicking button i.e on loading the form it should display the combobox. – Nihal Kumar Jun 08 '15 at 10:24
  • The button was just for testing and it did work, the error msg nonewithstanding. So there must be something else be going on to either prevent the code from working or (imo more likely ) reverse it. Can't help you there as it is your project. There are a few event that you might trace into, but it is hard to tell which is the best one to catch the moment when things get reverted: ColumnAdded, ColumnDefaultCellStyleChanged, DefaultCellStyleChanged, CellStyleChanged, or DataSourcechanged.. – TaW Jun 08 '15 at 11:05
  • I tried but no luck :( I am using DataBindingComplete event but this event occurs a lot many times. I think it should occur once the DGV draw completes with databinding. – Nihal Kumar Jun 08 '15 at 12:22
  • Well it occurs quite often indeed..: _This event is raised when the contents of the data source change or when the value of the DataSource, DataMember, or BindingContext property changes._ – TaW Jun 08 '15 at 12:52
  • Hm, looking at [this post](http://stackoverflow.com/questions/24329964/alternative-to-datagridview-databindingcomplete-event) I suggest to try a similar appoach: Instead of the flag used there (not very successfully) you could try to test the type of the cell in question. If it isn't combo call the combe creation code, if it still is don't . Also console.write out the times you hit that event to get a feeling for it.. – TaW Jun 08 '15 at 12:53