Abhishek Abhishek - 6 days ago 4
C# Question

How do I get the Selected Item Value from Data Grid Combo Box Column

I have a Data Grid With a text column and a Combo Box Column.

The Data Grid Columns is as Follows:

<DataGrid.Columns>
<DataGridTextColumn Header="ID"
Binding="{Binding ID}"
Width="0.5*" />
<DataGridComboBoxColumn Header="Threshold"
x:Name="Threshold"
SelectedItemBinding="{SelectedValue}"/>
</DataGrid.Columns>


For the ComboBox Column I am using a List as an ItemSource.

Threshold.ItemsSource = MaxThreshold; //MaxThreshold is a List


So every row has a ComboBox which consists of the List values.

The user selects a particular row and then selects the combo box in that particular row.
I want to get the value of the item selected in the ComboBox in c#.

Thanks.

Answer

To do the job you want you first need a data model, that is, in your case this model must contain two properties, and this model implements the INotifyPropertyChanged interface to notify the UI that property values They have changed. Below is the model that fits in your example, I called the model MyModel:

public class MyModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private object _id;
        public object ID 
        {
            get { return _id; }
            set 
            { 
                _id = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("ID"));
            }
        }

        private object _selectedItem;
        public object SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
            }
        }

        public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
    }

Note: You must change the type of attributes and properties for the type it deems necessary.

Your DataGrid would be something like the XAML shown below (the properties that fill the DataGrid and the ComboBox are in the .cs file of the window):

<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" 
                  ItemsSource="{Binding MyDataGridItems,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">
            <DataGrid.Columns >
                <DataGridTextColumn Header="   ID"  
                        Binding="{Binding ID}" 
                        Width="0.5*" />
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox x:Name="Threshold" 
                                      SelectedItem="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                      ItemsSource="{Binding MaxThreshold,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

In Window .cs file we have the following code snippet:

 public List<MyModel> MyDataGridItems { get; set; }
 public List<object> MaxThreshold { get; set; }

In the constructor of Window you can initialize the lists:

myDataGridItems = new List<MyModel>()
{
      new MyModel(){ID=1},
      new MyModel(){ID=2},
      new MyModel(){ID=3},
      new MyModel(){ID=4},
      new MyModel(){ID=5},
};

MaxThreshold = new List<object>()
{
    "Item 1",
    "Item 2",
    "Item 3",
    "Item 4",
    "Item 5"
};

And to pick up the selected item of combobox in the particular row of DataGrid you can iterate through the items of the DataGrid, or access the desired row directly, is an example:

//iterate rows
foreach(MyModel model in myDataGrid.Items)
{
    var selecteditem = model.SelectedItem;//here you have selected item
}

//access directly
var model = myDataGrid.Items[0] as MyModel;
if(model!=null)
   var selecteditem = model.SelectedItem;//here you have selected item