Home Forums WinForms controls Xceed Grid for WinForms GridCheckBox in a InsertRow Reply To: GridCheckBox in a InsertRow

User (Old forums)
Member
Post count: 23064

Use the class below as a viewer, instead of GridCheckBox. You can only use it as a viewer, not as an editor. Set the <b>DefaultValue</b> property to the value you want to see displayed instead of <i>indeterminate</i>.
Use it like this:
Grid1.Columns[“View”].CellViewer = new CustomGridCheckBoxViewer();
<code>
public class CustomGridCheckBoxViewer : ICellViewer
{
private ICellViewer viewer;
private bool defaultValue;

public CustomGridCheckBoxViewer()
{
viewer = new GridCheckBox();
defaultValue = true;
}

public bool DefaultValue
{
get { return defaultValue; }
set { defaultValue = value; }
}

#region ICellViewer Members
public int GetFittedHeight(Cell cell, Xceed.Grid.AutoHeightMode mode) { return viewer.GetFittedHeight(cell, mode); }
public int GetFittedWidth(Cell cell) { return viewer.GetFittedWidth(cell); }
public bool PaintCellValue(GridPaintEventArgs e, Cell cell)
{
if (!(cell.Value is bool)) cell = new CheckBoxCell(cell, defaultValue);
return viewer.PaintCellValue(e, cell);
}
#endregion

private class CheckBoxCell : Cell
{
private bool value;
public CheckBoxCell(Cell cell, bool value):base(cell)
{
this.value = value;
HorizontalAlignment = cell.HorizontalAlignment;
VerticalAlignment = cell.VerticalAlignment;
BackColor = cell.GetBackColorToPaint();
ForeColor = cell.GetForeColorToPaint();
}

protected override Cell CreateInstance() { return new CheckBoxCell(this, value); }
protected override object GetValue() { return value; }
protected override void SetValue(object value) { }
}
}
</code>

Imported from legacy forums. Posted by Tommy (had 218 views)