ComponentOne DataGrid

Posted by: mayur.purandare on 24 October 2019, 6:02 pm EST

    • Post Options:
    • Link

    Posted 24 October 2019, 6:02 pm EST

    We have binded item source to observable collection of custom type and When we double click on any row we want to take that row data in our custom type.At that time we are getting casting error.Can we get some to sample to take selected row data from data grid.

  • Posted 24 October 2019, 8:06 pm EST

    Hi Mayur,

    It seems like you are trying to cast the DataGridRow into your custom type and hence the issue. C1DataGrid row provides the bound data item by its DataItem property.

    var selectedRowItem = dataGrid.Rows[dataGrid.SelectedIndex].DataItem as CustomType;
    

    Or alternatively, you can directly use the SelectedItem for this purpose:

    private void DataGrid_SelectionChanged(object sender, C1.WPF.DataGrid.DataGridSelectionChangedEventArgs e)
    {
     var selectedRowItem = dataGrid.SelectedItem as CustomType;
     ...
    }
    
    

    Please refer to the attached sample for the same.

    Thanks,

    Basant

    prj_SelectedRowItem.zip

  • Posted 29 October 2019, 7:21 pm EST

    Hi,

    How can i achieve this in MVVM pattern and not in code behind pattern. We are doing it through events to commands and passing command parameter as selectedItems.

  • Posted 30 October 2019, 11:56 pm EST

    Hi Mayur

    For this, you can simply pass the SelectedItem of C1DataGrid to your command parameter.

    <Button Name="btnDisplaySelected" Content="Display Selected Item" Command="{Binding DisplaySelectedItemCommand}" 		CommandParameter="{Binding ElementName=dataGrid, Path=SelectedItem}"/>
    <c1:C1DataGrid x:Name="dataGrid" ItemsSource="{Binding Products}" IsReadOnly="True" SelectionMode="SingleRow" Grid.Row="1"/>
    

    Please refer to the attached sample for verification. You can use the same approach for the WPF event to command pattern. Also, If needed, you can bind the C1DataGrid.SelectedItem to some property of your ViewModel.

    Thanks,

    Basant

    prj_SelectedRowItem_Mod.zip

  • Posted 31 October 2019, 10:37 pm EST

    Hi,

    We have added one column as checkbox column and we want to allow multiple row selection on check of checkbox.

    Is it possible in MVVM.

  • Posted 3 November 2019, 5:01 pm EST

    Hi Mayur,

    As per my understanding, you want to select/deselect a row when a checkbox is checked/unchecked in that particular row and allowing multiple rows to be selected. If so then please refer to the answer below otherwise please correct me where I am mistaken.

    Since it will require to handle the check/unchek events of the checkboxes so, we’ll have to handle the LoadedCellPresenter events as follows:

    private void DataGrid_LoadedCellPresenter(object sender, C1.WPF.DataGrid.DataGridCellEventArgs e)
    {
     if(e.Cell.Column.Name == "IsSelected")
     {
      (e.Cell.Presenter.Content as CheckBox).Tag = e.Cell;
      (e.Cell.Presenter.Content as CheckBox).Checked += MainWindow_Checked;
      (e.Cell.Presenter.Content as CheckBox).Unchecked += MainWindow_Unchecked;
     }
    }
    

    Now, upon checkbox checked/unchecked events we’ll need to update the collection of selected items. Since we need to maintain selected items in a customized way so, we’ll have to keep the collection of selected items in our ViewModel and use it as follows:

    private void MainWindow_Unchecked(object sender, RoutedEventArgs e)
    {
     C1.WPF.DataGrid.DataGridCell cell = (sender as CheckBox).Tag as C1.WPF.DataGrid.DataGridCell;
     dataGrid.Selection.Remove(cell.Row, cell.Row);
     vm.SelectedProducts.Remove(cell.Row.DataItem as Product);
    }
    
    private void MainWindow_Checked(object sender, RoutedEventArgs e)
    {
     C1.WPF.DataGrid.DataGridCell cell = (sender as CheckBox).Tag as C1.WPF.DataGrid.DataGridCell;
     vm.SelectedProducts.Add(cell.Row.DataItem as Product);
    }
    

    Update DataGrid selection when our collection of selected items is updated:

    private void SelectedProducts_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
     foreach(var item in vm.SelectedProducts)
     {
      var row = dataGrid.Rows.FirstOrDefault(r => r.DataItem == item);
       dataGrid.Selection.Add(row, row);
     }
    }
    

    Please refer to the attached sample for the same.

    Regards,

    Basant

    prj_SelectedRowItem_Mod2.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels