How to hide particular cell in Datagrid based on some condition?

Posted by: jagdeeshk on 5 December 2019, 3:27 am EST

    • Post Options:
    • Link

    Posted 5 December 2019, 3:27 am EST - Updated 4 October 2022, 9:01 am EST

    In my project am trying to hide some checkboxes in Datagrid without effecting the background color based on some condition.

    Below image i would like to hide circled checkboxes with out effecting the Background color.

    Resulted Datagrid should be like this

    SoureCode Project Code.zip

    Thank you.

  • Posted 5 December 2019, 5:57 am EST

    Hi,

    In order to hide the cell element, you can make use of C1DataGrid.LoadedCellPresenter event and toggle its visibility as per your condition:

    private void C1DataGrid_LoadedCellPresenter(object sender, C1.WPF.DataGrid.DataGridCellEventArgs e)
    {
     if(e.Cell.Presenter.Content!=null && e.Cell.Presenter.Content is CheckBox)
     {
      var chb = e.Cell.Presenter.Content as CheckBox;
      if((e.Cell.Row.DataItem as Employee).Department == "Dev Tools")
      {
       chb.Visibility = Visibility.Collapsed;
      }
     }
    }
    

    Please refer to the attached modified sample for the same. Also, for a variety of examples for C1DataGrid, you can refer to the “DataGridSamples” product sample from “Documents\ComponentOne Samples\WPF\C1.WPF.DataGrid\CS\DataGridSamples”.

    Just to inform you, if you want to specify the alternate rows background/foreground of your choice then you can simply use the following properties: RowBackground, RowForeground, AlternatingRowBackground, AlternatingRowForeground at C1DataGrid.

    Regards,

    Basant

    HideCheckBoxInCell.zip

  • Posted 5 December 2019, 9:52 am EST - Updated 4 October 2022, 9:01 am EST

    Hi Basant,

    In my case row count is more than 500 and every row having more than one checkbox column. I applied above logic but am facing two issues, when click on hidden checkbox area checkbox is showing and another issue is when scrolling continuously showing few check-boxes automatically.

    Grid View before scrolling :

    Grid View after scrolling :

    Source Code : Test.zip

    Not only single checkbox just identifying purpose circled only one. Thanks for your immediate response.

    Thanks & Regards,

    Jagdeesh.

  • Posted 6 December 2019, 1:57 am EST

    Hi Jagdeesh,

    Thanks for providing the sample and screenshots.

    1. Regarding hidden Checkboxes being shown when clicked on the cell:

    Since we explicitly hide the cell content and cell clicks are handled by DataGrid (not knowing that cell element is hidden) to make a cell editable hence the behavior. To resolve this issue, you can handle the C1DataGrid.BeginningEdit event and cancel the edit if the cell meets some specified criteria.

    private void C1DataGrid_BeginningEdit(object sender, C1.WPF.DataGrid.DataGridBeginningEditEventArgs e)
    {
     if(IsCellSatisfyingCondition(c1DataGrid[e.Row, e.Column]))
     {
      e.Cancel = true;
     }
    }
    

    2. Regarding Scrolling issue:

    C1DataGrid reuses the cell elements for better performance. So, we also need to set the checkboxes’ visibility back to visible if the cell does not meet the specified criteria.

    private void C1DataGrid_LoadedCellPresenter(object sender, C1.WPF.DataGrid.DataGridCellEventArgs e)
    {
     if(e.Cell.Presenter.Content!=null && e.Cell.Presenter.Content is CheckBox)
     {
      var chb = e.Cell.Presenter.Content as CheckBox;
      chb.Visibility = IsCellSatisfyingCondition(e.Cell) ? Visibility.Collapsed : Visibility.Visible;
     }
    }
    
    

    Please verify the above with the attached modified sample.

    Regards,

    Basant

    prj_HideCheckboxInCell_Mod.zip

  • Posted 6 December 2019, 2:34 am EST

    Thanks a lot now working fine.

    Thanks & Regards,

    Jagdeesh.

Need extra support?

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

Learn More

Forum Channels