Posted 16 March 2025, 8:15 am EST
How to set the checkbox for the Unbound Column?
Forums Home / ComponentOne / WPF Edition
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
Posted 6 August 2026, 5:39 am EST
What is the difference between GridControlRange and GridCellRange.
When I did flexGrid.GetCellView(new GridControlRange(0, col)) , it refers to columnheadecell instead of row 0 of the grid, there is no way to mention the celltype in case of GridControlRange
David
Posted 7 August 2026, 7:06 am EST
Hi David,
The GridControlRange represents a grid-level range, which includes all cells in the grid, including the headers. In contrast, GridCellRange represents a cell-level range and does not include the headers.
Therefore:
To simplify the range calculation on your end, you can create the GridControlRange object by manually accounting for the header row/column offsets using the following code:
int row = 0;
int col = 0;
bool isChecked = true;
var columnHeaderRowsCount = flexGrid.ColumnHeaders.VisibleRowsCount;
var RowHeaderColumnsCount = flexGrid.RowHeaders.VisibleColumnsCount;
row += columnHeaderRowsCount;
col += RowHeaderColumnsCount;
GridControlCellView cellView = flexGrid.GetCellView(new GridControlRange(row, col));
if (cellView.Content is CheckBox chk)
{
chk.IsChecked = isChecked;
}
Best Regards,
Kartik