DataGrid: How to manipulate selected item

636 views Asked by At

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.

3

There are 3 answers

4
Sheridan On

To test whether your code example actually works or not, try changing it to this and running your application:

for (int i = 0; i < dgResults.Items.Count; i++)
{
    var x = dgResults.GetCell(i, 0).Content as System.Windows.Controls.ComboBox;
    x.SelectedIndex = comboBoxItemsSource.Items.Count - 1;
}

If the ComboBoxes 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 the string in the Items 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 this for loop to verify that x is indeed the correct cell?

0
WiiMaxx On

If you don't bother another cast you may could do this

for (int i = 0; i < dgResults.Items.Count; i++)
{
    var x = dgResults.GetCell(i, 0).Content as System.Windows.Controls.ComboBox;

    var array = x.ItemsSource as string[] ;
    x.SelectedItem = array.Where(s => s == "B").FirstOrDefault();
}

EDIT: ok this should work now

working sample: XAML

<Window x:Class="simpletest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <x:Array x:Key="ReminderValues" Type="sys:String">
            <sys:String>A</sys:String>
            <sys:String>B</sys:String>
            <sys:String>C</sys:String>
            <sys:String>D</sys:String>
        </x:Array>
        <x:Array x:Key="count" Type="sys:String">
            <sys:String>A</sys:String>
        </x:Array>
    </Window.Resources>
    <Grid>
        <DataGrid Name="dgResults" ItemsSource="{StaticResource count}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridComboBoxColumn ItemsSource="{StaticResource ReminderValues }" />
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="428,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

CS

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < dgResults.Items.Count; i++)
        {
            var x = dgResults.GetCell(i, 0).Content as System.Windows.Controls.ComboBox;

            var array = x.ItemsSource as string[];
            x.SelectedItem = array.Where(s => s == "B").FirstOrDefault();
        }
    }
}
0
dotNET On

Finally figured it out after taking some sleep. If you've got an UNBOUND DataGridComboBoxColumn in your grid (i.e. the column doesn't bind to a column in the underlying data source) and its ItemsSource property is bound to an array or something, you must add the following to your DataGrid declaration:

SelectedItemBinding="{Binding /}"

The slash character (/) above represents the current item itself, which is what we want our SelectedItem to be.

Now my DataGrid displays values correctly and once the cell gets focus, the ComboBox appears with the correct value selected.