DataGrid: how to manipulate a selected item

It should be simple, but all my searches lead to binding solutions, which is not my case.

I have a DataGrid that has a DataGridComboBoxColumn . This property of the ItemsSource column is bound to an array of rows. I use the startup loop to set the SelectedItem 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 specific cell of this column, the drop-down menu appears in the cell and is correctly filled with all elements of the array, but the current text of the drop-down text is empty, i.e. it does not automatically select the appropriate item from the drop-down list. What am I missing?

EDIT

Here is the relevant part 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 associated with the underlying DataColumn or something, although the entire DataGrid IS is bound to a DataTable. In addition, I know for sure that this is not a spelling problem.

+3
source share
3 answers

Finally realized this after some sleep. If you have a UNBOUND DataGridComboBoxColumn in your grid (i.e. a column does not bind to a column in the underlying data source), and its ItemsSource property is bound to an array or something else, you should add the following: your DataGrid declaration:

 SelectedItemBinding="{Binding /}" 

The slash character (/) above is the current element for which we want our SelectedItem to be.

Now my DataGrid displays the values ​​correctly, and as soon as the cell receives focus, the ComboBox appears with the correct value selected.

+1
source

To check if your sample code really works or not, try changing it to it and launching the 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 last option is selected for the ComboBox es parameter, this code works well. If not, then you have a problem. If this works, the problem may be that you didn’t exactly match the string in the Items collection that you want to select ... remember, if even one character is in the wrong case, t will not be selected.

If the above code doesn't work, maybe you need to test your GetCell method GetCell ... have you added a breakpoint to this for loop to make sure x really the correct cell?

0
source

If you are not worried about another actor, you can do it

 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: this should work fine 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
source

All Articles