Multiple CheckBox controls in a DataGrid cell

Paweł Łepko 26 Reputation points
2021-02-19T13:22:30.387+00:00

Hello,

In the DataGrid control I would like to add a column in which each cell would contain a list of checkbox controls (for each cell there can be a different number of such checkboxes with different descriptions). I don't know how to define such a column with an undefined number of checkboxes in a cell.

Please help.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,626 Reputation points
    2021-02-22T03:18:06.43+00:00

    I will show you a sample of adding undefine number of CheckBox in a DataGrid cell.
    Xaml code:

     <Grid>  
            <Grid.Resources>  
                <DataTemplate x:Key="DataTemplate">  
                    <ListBox ItemsSource="{Binding Checks}">  
                        <ListBox.ItemTemplate>  
                            <DataTemplate>  
                                <WrapPanel>  
                                    <CheckBox IsChecked="{Binding Check}"/>  
                                    <TextBlock Text="{Binding CheckContent}"/>  
                                </WrapPanel>  
                            </DataTemplate>  
                        </ListBox.ItemTemplate>  
                    </ListBox>  
                </DataTemplate>  
            </Grid.Resources>  
            <DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" ItemsSource="{Binding DataGridDataCollection}">  
                <DataGrid.Columns>  
                    <DataGridTemplateColumn Header="Checks" Width="150" CellTemplate="{StaticResource DataTemplate}" />  
                </DataGrid.Columns>  
            </DataGrid>  
        </Grid>  
    

    The cs code is:

    public partial class Window1 : Window, INotifyPropertyChanged  
        {  
            public event PropertyChangedEventHandler PropertyChanged;  
      
            private ObservableCollection<DataGridRowData> _DataGridDataCollection;  
            public ObservableCollection<DataGridRowData> DataGridDataCollection  
            {  
                get => _DataGridDataCollection;  
                set  
                {  
                    _DataGridDataCollection = value;  
                    OnPropertyChanged();  
                }  
            }  
      
            public Window1()  
            {  
                DataGridDataCollection = new ObservableCollection<DataGridRowData>();  
                InitializeComponent();  
                this.DataContext = this;  
                addChecks();  
            }  
      
            // adds data to the DataGrid  
            private void addChecks()  
            {  
                DataGridDataCollection.Add(  
                new DataGridRowData(new List<DataGridCellData>   
                {   
                    new DataGridCellData(false,"Content 1_1"),   
                    new DataGridCellData(true,"Content 1_2"),   
                    new DataGridCellData(false,"Content 1_3"),   
                }));  
      
                DataGridDataCollection.Add(  
                new DataGridRowData(new List<DataGridCellData>  
                {  
                    new DataGridCellData(false,"Content 2_1"),  
                    new DataGridCellData(true,"Content 2_2"),  
                    new DataGridCellData(false,"Content 2_3"),  
                    new DataGridCellData(false,"Content 2_4"),  
                    new DataGridCellData(true,"Content 2_5")  
                }));  
      
                DataGridDataCollection.Add(  
               new DataGridRowData(new List<DataGridCellData>  
               {  
                    new DataGridCellData(false,"Content 3_1"),  
                    new DataGridCellData(true,"Content 3_2")  
               }));  
            }  
      
      
            protected void OnPropertyChanged([CallerMemberName] string name = null)  
            {  
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
            }  
        }  
      
        public class DataGridRowData  
        {  
            public List<DataGridCellData> Checks { get; set; }  
      
            public string CheckContent { get; set; }  
            public DataGridRowData(List<DataGridCellData> checks)  
            {  
                Checks = new List<DataGridCellData>();  
                foreach (var b in checks)  
                {  
                    Checks.Add(b);  
                }  
            }  
        }  
      
        public class DataGridCellData  
        {  
            public bool Check { get; set; }  
            public string CheckContent { get; set; }  
            public DataGridCellData(bool check,string checkContent)  
            {  
                Check = check;  
                CheckContent = checkContent;  
            }  
        }  
    

    The result picture is:
    70367-capture.png

    Is it what you want? If it is not meet your needs, please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Yuriy Elkine 0 Reputation points
    2024-08-26T17:53:02.42+00:00

    Hello,
    I am looking for a similar functionality. This is a very good example.
    Just we need the checklist: 1) to be a dropdown checklist, 2) after dropdown closed - cell value would be a coma separated list of checked items from dropdown.
    Similar functionality from Delphi:

    User's image

    Or, alternatively, it can be a DataGridViewComboBoxColumn,
    just with ability to multiselect items from dropdown (we have it in Java part of application).

    I thought, maybe to try add CheckBox as an item to DataGridViewComboBoxColumn, but I would need to create a cell value as a coma separated list.

    Thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.