This must be simple but all my searching leads to binding-based solutions, which is not my case.
I have a DataGrid
in which there is a DataGridComboBoxColumn
. This column's ItemsSource
property is bound to a string array. I use a loop in the startup to set the SelectedItem
of this column for each row of my DataGrid through this code:
for (int i = 0; i < dgResults.Items.Count; i++)
{
var x = dgResults.GetCell(i, 0).Content as System.Windows.Controls.ComboBox;
x.SelectedItem = "One of the items of my array";
}
GetCell()
is an extension method that I grabbed from here.
Now the problem is that when I click on a particular cell of this column, the dropdown appears in the cell and is correctly populated with all array items, but the dropdown's current text is empty, i.e. it doesn't automatically select the corresponding item from the dropdown. What am I missing?
EDIT
Here is the relevant portion of my DataGrid:
<DataGrid x:Name="dgResults" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{StaticResource ReminderValues }" />
</DataGrid.Columns>
</DataGrid>
As you can see, this particular column is not bound to an underlying DataColumn
or something, although the whole DataGrid IS bound to a DataTable. Also, I know for sure that this is not a spelling issue.
To test whether your code example actually works or not, try changing it to this and running your application:
If the
ComboBox
es all have the last option selected, then this code works well. If not, then you have a problem. If it does work, the problem could be that you didn't exactly match thestring
in theItems
collection that you want to be selected... remember, if even one character is in the wrong case, it won't match and therefore won't be selected.If the above code does not work, then maybe you need to test your
GetCell
method further... have you put a break point in thisfor
loop to verify thatx
is indeed the correct cell?