This sample shows how to bind checkboxes in HierachyColumn:
[c#]
using NineRays.Windows.Forms;
using NineRays.Windows.Forms.Data;
using NineRays.Windows.Forms.Grids;
using NineRays.Windows.Drawing;
//....
private void Form1_Load(object sender, System.EventArgs e)
{
InitFlyGrid(flyGrid);
}
public class BoundCheckBoxColumn : HierachyColumn
{
public BoundCheckBoxColumn(string name) : base(name){}
public BoundCheckBoxColumn(string name, string fieldName) : base(name, fieldName){}
private string checkBoxFieldName = string.Empty;
[DefaultValue(\"\")]
public string CheckBoxFieldName
{
get
{
return checkBoxFieldName;
}
set
{
if (checkBoxFieldName != value)
{
checkBoxFieldName = value;
base.OnChanged(InvalidationMode.FullColumn, false);
}
}
}
public override void OnCheckBoxClick(FlyGrid flyGrid, NodeBase node)
{
if (CheckBoxesReadOnly && CheckBoxFieldName != string.Empty)
{
CheckBoxState cbs = GetCheckBoxstate(node);
DataRowView drv = node.Value as DataRowView;
if (drv != null)
{
if ((cbs & CheckBoxState.Checked) != 0)
cbs &= ~CheckBoxState.Checked;
else
cbs |= CheckBoxState.Checked;
if ((cbs & CheckBoxState.Grayed) != 0)
cbs &= ~CheckBoxState.Grayed;
drv[CheckBoxFieldName] = cbs;
}
}
else
{
base.OnCheckBoxClick(flyGrid, node);
}
}
protected override CheckBoxState GetCheckBoxstate(NodeBase node)
{
if (CheckBoxFieldName != string.Empty)
{
DataRowView drv = node.Value as DataRowView;
if (drv != null)
{
object cellvalue = drv[CheckBoxFieldName];
return cellvalue != null && cellvalue != DBNull.Value ?
(CheckBoxState)cellvalue :
CheckBoxState.None;
}
}
return base.GetCheckBoxstate (node);
}
}
private void InitFlyGrid(FlyGrid flyGrid)
{
flyGrid.BeginInit();
try
{
BoundCheckBoxColumn boundCheckBoxColumn = new BoundCheckBoxColumn(\"Value\");
//setup column
boundCheckBoxColumn.CheckBoxFieldName = \"CheckBoxValue\";
boundCheckBoxColumn.ShowCheckBoxes = true;
//fit to width
boundCheckBoxColumn.FitMode = ColumnFitMode.Spring;
flyGrid.Columns.Options |= ColumnsOptions.FitToWidth;
//hide AddNewRow row
flyGrid.Rows.Options &= ~ RowsOptions.ShowAddNewRow;
//add column
flyGrid.Columns.Items.Add(boundCheckBoxColumn);
//create and setup datatable
DataTable dt = new DataTable(\"Data\");
DataColumn dc = new DataColumn(\"Value\", typeof(string));
DataColumn dcb = new DataColumn(\"CheckBoxValue\", typeof(ButtonState));
dt.Columns.Add(dc);
dt.Columns.Add(dcb);
//fill data
for(int i=0; i < 10; i++)
{
object[] rowdata =
{
\"new row\" + i,
i % 2 == 0 ? CheckBoxState.Checked : CheckBoxState.Checked | CheckBoxState.Grayed
};
dt.Rows.Add(rowdata);
}
flyGrid.Rows.DataSource = dt;
}
finally
{
flyGrid.EndInit();
}
}