Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Data bound checkbox of the HierarchyColumn

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Data bound checkbox of the HierarchyColumn
Link Posted: 27-Jun-2006 03:32
Hi,

Is it possible to bind the check box, which is displayed in each cell of the HierarchyColumn (when ShowCheckBoxes==true), to the data field of the same data source assigned to the FlyGrid.Rows.DataSource?

Thank you.
Link Posted: 27-Jun-2006 06:39
This sample shows how to bind checkboxes in HierachyColumn:
[c#]
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)
  {
    base.OnCheckBoxClick(flyGrid, node);
    DataRowView drv = node.Value as DataRowView;
    if (drv != null)
    {
      ButtonState cbs = node.Checked ? ButtonState.Checked : ButtonState.Normal;
      drv[CheckBoxFieldName] = cbs;
    }
  }
  
  protected override void PaintCheckBox(CellDrawInfo dci)
  {
    if (CheckBoxFieldName != string.Empty)
    {
      Rectangle r = base.CheckBoxRect(dci.node, dci.cellRect, dci.RightToLeft);
      DataRowView drv = dci.node.Value as DataRowView;
      if (drv != null)
      {
        object cellvalue = drv[CheckBoxFieldName];
        ButtonState cbs = (ButtonState)cellvalue;
        cbs |= ButtonState.Flat;
        ControlPaint.DrawCheckBox(dci.g, r, cbs);
      }
    }
    else
    {
      base.PaintCheckBox(dci);
    }
  }
}

private void Form1_Load(object sender, System.EventArgs e)
{
  InitFlyGrid(flyGrid);
}

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 ? ButtonState.Checked : ButtonState.Checked | ButtonState.Inactive
          };
      dt.Rows.Add(rowdata);
    }
    flyGrid.Rows.DataSource = dt;    
  }
  finally
  {
    flyGrid.EndInit();
  }
}
Link Posted: 27-Jun-2006 11:15
We've made some improvements to simplify control of data bound checkboxes and support unified checkboxes drawing, with the nearest FlyGrid.Net update you can use following code:
[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();
  }
}
Link Posted: 27-Jun-2006 20:03
Thank you very much!