-1


I want to know if there is a way to set index of ComboBox that is already in DataGridView column. I don't want to add new Column and/or Remove existing one.

Let's say I have four columns (e.g. Employee Info) in DataGridView as:

ID   |    Name   |   Address   |    Job

Now, Job attribute is of type ComboBox has some known and valid values. I have tried via DataTable and DataGridViewComboBoxCell methods but didn't work as required.

What should be done at this point? Here is the code:

        dtGridView.Rows.Add();
        dtGridView.Rows[row].Cells["ID"].Value = ID;
        dtGridView.Rows[row].Cells["Name"].Value = Name;
        dtGridView.Rows[row].Cells["Address"].Value = Address;
        // Below Line doesn't work
        dtGridView.Rows[row].Cells["Job"].Value = ????????

Keep in mind that dtGridView is Read-Only and value must be assigned via code i.e. set Index of ComboBox.

MIftikharK
  • 67
  • 8
  • Your question is confusing. Do you want to add items to the combo at runtime, or set the value? – stuartd Nov 15 '16 at 16:31
  • @stuartd: Oops, my mistake... I don't want to add new values just taking any value from within the list. – MIftikharK Nov 15 '16 at 16:53
  • What's the datasource of the combo, and what error do you get when you set the value? – stuartd Nov 15 '16 at 16:58
  • I get error: _System.ArgumentException: DataGridViewComboBoxCell value is not valid._ – MIftikharK Nov 15 '16 at 17:09
  • It sounds like the value you're trying to set does not exist in the data source the combo ius bound to, or is the wrong type. – stuartd Nov 15 '16 at 17:11
  • I know that's type error... Value assigned is of type string while column in of type ComboBox but even if I assign ComboBox Object to it. Still the same error. – MIftikharK Nov 15 '16 at 17:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128193/discussion-between-miftikhark-and-stuartd). – MIftikharK Nov 15 '16 at 17:24
  • 1
    There are lots of questions that address this exact error - http://stackoverflow.com/questions/654829/datagridviewcomboboxcell-binding-value-is-not-valid, http://stackoverflow.com/questions/13461885/error-datagridviewcomboboxcell-value-is-not-valid-datasource-is-list-of-basi, http://stackoverflow.com/questions/19503306/datagridviewcomboboxcell-claims-value-is-not-valid etc – stuartd Nov 15 '16 at 17:27
  • Thanks Man! I tried most of the solutions available on StackOverflow. After reading them I asked this question but still can't figure it out. – MIftikharK Nov 15 '16 at 17:32

1 Answers1

0

DataGridViewComboBoxColumn and DataGridViewComboBoxCell both together solved my problem. Values are now stored in Enum and taking from there for both ComboBoxes on FORM and as well as DataGridViewComboBox

MIftikharK
  • 67
  • 8