I am attempting to populate a DataGridViewComboBoxColumn with a list of strings then select one of them based upon their value on form load.
A simple task one would think, but I just can't get it right.
I am populating a DataGridViewComboBoxColumn with strings as such without a problem:
ComboBoxColumn.Items.AddRange("Mr.", "Ms.", "Mrs.", "Dr.");
I also seem to be able to add it to the DataGridView without a problem (This is incorrect see Edit):
ExampleDataGrid.Rows.Add("", ComboBoxColumn, 1000, "");
Now I want to set "Mr." to be selected on load. Other posts suggest that I should be able to simply use:
ExampleDataGrid.Rows[i].Cells["ExampleColumnName"].Value = "Mr.";
But whenever I use it, I get an error that tells me the value is not valid. Is there something I'm missing?
I can however use this to get the set value without issue:
string Title = ExampleDataGrid.Rows[i].Cells["ExampleColumnName"].Value;
I had a look at the documentation but it doesn't seem to mention how to actually use .Value in this context. Microsoft Docs
Any thoughts on where I am going wrong would be great.
Edit:
The issue I was having was caused by me setting the ComboBoxItems in the "ExampleDataGrid.Rows.Add()". This should actually contain the value you want to set. e.g.
ExampleDataGrid.Rows.Add("", "Mr.", 1000, "");