WPF flexgrid Unbound templated Checkbox column

Posted by: david on 16 March 2025, 8:15 am EST

  • Posted 16 March 2025, 8:15 am EST

    How to set the checkbox for the Unbound Column?

  • Posted 17 March 2025, 5:02 am EST

    Hi David,

    You can create checkboxes for the boolean columns, by implementing a custom CellFactory for the unbound FlexGrid control. Here is the code snippet for the same:

    public class CheckBoxCellFactory : GridCellFactory
    {
        FlexGrid grid;
        public CheckBoxCellFactory(FlexGrid fg)
        {
            grid = fg;
        }
        public override FrameworkElement CreateCellContent(GridCellType cellType, GridCellRange range, object cellContentType)
        {
            if (cellType == GridCellType.Cell && grid.Columns[range.Column].ColumnName == "Select")
            {
                CheckBox checkBox = new CheckBox
                {
                    Margin = new Thickness(5),
                    HorizontalAlignment = HorizontalAlignment.Center
                };
                // Set the checkbox state based on the grid's cell value
                object cellValue = grid[range.Row, range.Column];
                if (cellValue is bool isChecked)
                {
                    checkBox.IsChecked = isChecked;
                }
                // Handle CheckBox Click Event
                checkBox.Checked += (s, e) => grid[range.Row, range.Column] = true;
                checkBox.Unchecked += (s, e) => grid[range.Row, range.Column] = false;
                return checkBox;
            }
            return base.CreateCellContent(cellType, range, cellContentType);
        }
    }

    Kindly refer to the attached sample for full implementation. See CheckBoxCellDemo.zip

    Thanks & regards,

    Aastha

  • Posted 18 March 2025, 3:51 am EST

    Hi Aastha

    Thanks for your reply.

    I need to check/uncheck the checkboxes in code behind, not at creation.

    Thanks & regards

    David

  • Posted 19 March 2025, 2:22 am EST

    Hi David,

    You can fetch the Content of GridControlCellView returned via FlexGrid’s GetCellView() method. Here is the code snippet for the same:

    // You can initialize these variables based on your requirement
        int row = 2; 
        int col = 0;
        bool isChecked = true;
        // code to check/uncheck the cell's checkbox via code
        GridControlCellView cellView = flexGrid.GetCellView(new GridControlRange(row, col));
        if (cellView.Content is CheckBox chk) 
        {
            chk.IsChecked = isChecked;
        }

    Kindly refer to the attached sample for full implementation. See CheckBoxCellDemo_Mod.zip

    Thanks & regards,

    Aastha

Need extra support?

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

Learn More

Forum Channels