views:

85

answers:

2

I am using MVVM light with silverlight 4. I am using EventToCommand to get selected rows from the datagrid. However i am not able to do it in reverse way i.e. from some defined value say 2 in view model how can I ask to place selection to row no 2 in the datagrid on view?'

My scenario I am using it for inserting new data in database. From ViewModel i am able to generate the new row in datagrid by adding one more item in ObservableCollection. Now I want to view to select that defined new row. How could I do that.

My logic is: 1. since we can have total number of data record we know which one is that last row. 2. By using the Last row's number I want to create some event that will tell datagrid to select that row.

I am working on this for few days and not getting any idea how it works using MVVM light.

A: 

Am not using MVVM Light, but if I was to act upon a selection in a row, I would do it this way, provided you have a control that is binded:

private void simpleButton_Click(object sender, RoutedEventArgs e)
{
    var cell = dataGrid1.SelectedItem as SimplePocoObject;
    if (cell != null) _context.RunReport(cell.ID, cell.ServerName);
    myDomainDataSource.Load(); // refresh grid
}
rob
A: 

I am sorry I hope you misunderstood my question. I want a button that will selected a newest row created in data grid (Normally it should be end of the datagrid but if we have paging it may be at last page's last row)

My datagrid is binded to datasource via, observableCollection in viewModel. I tried something like but it is not working as i expected and the selectedIndex is always -1 even Datapager.ItemCount is more than 1 :

private void Btn_Click(object sender, RoutedEventArgs e)
    {
       Datapager.PageIndex = Datapager.ItemCount /Datapager.PageSize;
       dataGrid1.SelectedIndex = Datapager.ItemCount;
    }
webKite