0

i want to get the selected row of datagrid i.e on click in mvvm.i have below code i want to use it in MVVM,but not able to do so.Please let me know how to convert below 3 line s in mvvm using relay comand and Icommand

DataRowView dataRow = (DataRowView)dgProjectComponents.SelectedItem;
            int index = dgProjectComponents.CurrentCell.Column.DisplayIndex;
            string ProjectComponentID = Convert.ToString(dataRow.Row.ItemArray[2].ToString());

**Xaml of datagrid:**


<DataGrid SelectedItem="{Binding SelectedRow}" Background="{Binding ElementName=gd,Path=Background}" ItemsSource="{Binding ManualDataTable}"   x:Name="dgProjectComponents">
                                                <DataGrid.Columns>
                                                    <DataGridTemplateColumn Width="50" IsReadOnly="True">
                                                        <DataGridTemplateColumn.CellTemplate>
                                                            <DataTemplate >
                                                                <TextBlock  Height="10" Width="10" Background="{Binding ColorDefinition}" ></TextBlock>
                                                            </DataTemplate>
                                                        </DataGridTemplateColumn.CellTemplate>
                                                    </DataGridTemplateColumn>
                                                    <DataGridTextColumn Binding="{Binding ProjectComponentID}" Visibility="Hidden" Width="100" Header="ProjectComponentID" />
                                                    <DataGridTextColumn Binding="{Binding Title}" IsReadOnly="True" Width="140" />



                                                    <DataGridTemplateColumn Width="50">
                                                        <DataGridTemplateColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock><Hyperlink Command="{Binding Path=DataContext.DelProjectComponent,ElementName=ProjectTabWindow}"><Image Source="/img/Close.png"   x:Name="imgProjectComponentDelete" Height="15" Width="20"></Image></Hyperlink></TextBlock>
                                                            </DataTemplate>
                                                        </DataGridTemplateColumn.CellTemplate>
                                                    </DataGridTemplateColumn>
                                                </DataGrid.Columns>
                                            </DataGrid>
                                            </DataGrid>
user3386790
  • 156
  • 4
  • 16

1 Answers1

1

You have data bound your ManualDataTable collection to the ItemsSource property of your DataGrid and your SelectedRow property to the SelectedItem property. If your SelectedRow property is of the same type as the items in your ManualDataTable collection, then you can use it to reference the selected item from the DataGrid.

Therefore, you have no need to find a certain column from a DataGridRow, because you can access the properties of your class as normal... so instead of your three lines of code, you can simply do this (assuming that you have an Id property in your class):

string ProjectComponentID = SelectedRow.Id;
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • :How could i get the index of current cell of the column the from above mentioned suggestion of yours? – user3386790 Jun 30 '14 at 12:44
  • I just showed you how. You use the name of the property that feeds that column in the UI... I quite clearly said *assuming that you have an Id property* as I assumed that would be the name of the correct property. – Sheridan Jun 30 '14 at 12:52