I have a DataGrid with data populated from an XML binding.
I want to know how I can obtain the selected row in C#, more specifically I want to return the value of the ‘ID’ from the selected row from my dataset.
I can get the entire row contents with:
var downloadlistselected = downloadList.SelectedValue.ToString();
However, I only want the contents from the first column.
Can anyone help?
XAML
<Grid.DataContext>
<XmlDataProvider Source="E:\downloader\downloadConfig.xml" XPath="/xservdownload/downloadItem"></XmlDataProvider>
</Grid.DataContext>
<DataGrid x:Name="downloadList" Height="191" VerticalAlignment="Top" ItemsSource="{Binding}" AutoGenerateColumns="False" AlternatingRowBackground="Gainsboro" IsReadOnly="True" SelectionChanged="DownloadListSelectionChanged" DataContext="{Binding}" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding XPath=ID}" Width="50"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding XPath=Name}" Width="350"></DataGridTextColumn>
<DataGridTextColumn Header="Status" Binding="{Binding XPath=Status}" Width="100"></DataGridTextColumn>
</DataGrid.Columns>
Something along those lines should do:
Key being that the selected item should be an
XmlNodefrom which you can get whatever you need.