Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 3

Getting selected item from DataGridComboBoxColumn C# XAML

Solved! Go to Solution.

Hi I've been asking this everywhere was wondering if GitHub Community had the answer

I've been trying to get a selected item from a combobox in a datagrid cell.

I have an automatically generated datagrid depending on a list of items. This is my code:

ObservableCollection<Eclass> obsCollection = new ObservableCollection<Eclass>(this.e);
DataGridE.DataContext = obsCollection;
DataGridE.Columns[0].IsReadOnly = true;
DataGridE.Columns[1].Header = "Esp (m)";
DataGridComboBoxColumn descrip = new DataGridComboBoxColumn();
descrip.ItemsSource = new List<string> { "G", "C"};
descrip.Header = "Description";
descrip.TextBinding = new Binding("Description");
DataGridE.Columns[2] = descrip;
//that is the code to generate the grid

//this is the code to get some of the items
 for (int i = 0; i < this.e.Count; i++){
TextBlock esp = DataGridE.Columns[1].GetCellContent(DataGridE.Items[i]) as TextBlock;

It works great for esp since I can then convert that to a value or a String. But for the combobox column which is the DataGridE.Columns[2] I have no idea how to get the selected item or index, preferably item.

I have tried doing something similar with getcellcontent but it just doesn't work.

Any help is appreciated.

2 Replies
Highlighted
Solution
Commander Lvl 2
Message 2 of 3

Re: Getting selected item from DataGridComboBoxColumn C# XAML

I think this has your answer.

 

To summarize:

GetCellContent returns the contained control. In the case of DataGridComboBoxColumn, the contained control is derived from ComboBox. I.e:

 

for (int i =0; i < this.e.Count; i++)
{
    ComboBox ele = DataGridE.Columns[2].GetCellContent(DataGridE.Items[i]) as ComboBox;
    var selectedItem = ele.Text;
}

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!

Copilot Lvl 2
Message 3 of 3

Re: Getting selected item from DataGridComboBoxColumn C# XAML




@fire-eggs wrote:

I think this has your answer.

 

To summarize:

GetCellContent returns the contained control. In the case of DataGridComboBoxColumn, the contained control is derived from ComboBox. I.e:

 

for (int i =0; i < this.e.Count; i++)
{
    ComboBox ele = DataGridE.Columns[2].GetCellContent(DataGridE.Items[i]) as ComboBox;
    var selectedItem = ele.Text;
}

 


I have asked in different forums, from Stack Overflow to the Visual Studio forum and this is the correct answer to my problem, this worked. Thank you so much!