sourcegrid 应用实例(全部来自官网下载的例子)——Nullable CheckBox and OnValueChanged Controller

 

源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsSample.GridSamples
{
    [Sample("SourceGrid - Advanced features", 51, "Nullable CheckBox and OnValueChanged Controller")]
    public partial class frmSample51 : Form
    {
        public frmSample51()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            grid1.Redim(30, 2);

            grid1[0, 0] = new SourceGrid.Cells.ColumnHeader("Checked");
            grid1[0, 1] = new SourceGrid.Cells.ColumnHeader("CheckStatus");

            SourceGrid.Cells.Editors.ComboBox combo = new SourceGrid.Cells.Editors.ComboBox(typeof(DevAge.Drawing.CheckBoxState));

            DependencyColumn boolToStatus = new DependencyColumn(1);
            boolToStatus.ConvertFunction = delegate(object valBool)
                                            {
                                                if (valBool == null)
                                                    return DevAge.Drawing.CheckBoxState.Undefined;
                                                else if ((bool)valBool == true)
                                                    return DevAge.Drawing.CheckBoxState.Checked;
                                                else
                                                    return DevAge.Drawing.CheckBoxState.Unchecked;
                                            };

            DependencyColumn statusToBool = new DependencyColumn(0);
            statusToBool.ConvertFunction = delegate(object valStatus)
                                            {
                                                DevAge.Drawing.CheckBoxState status =
                                                    (DevAge.Drawing.CheckBoxState)valStatus;

                                                if (status == DevAge.Drawing.CheckBoxState.Undefined)
                                                    return null;
                                                else if (status == DevAge.Drawing.CheckBoxState.Checked)
                                                    return true;
                                                else
                                                    return false;
                                            };

            for (int r = 1; r < grid1.RowsCount; r++)
            {
                grid1[r, 0] = new SourceGrid.Cells.CheckBox(null, null);
                grid1[r, 0].Editor.AllowNull = true;
                grid1[r, 0].AddController(boolToStatus);

                grid1[r, 1] = new SourceGrid.Cells.Cell(DevAge.Drawing.CheckBoxState.Undefined, combo);
                grid1[r, 1].AddController(statusToBool);
            }

            grid1.AutoSizeCells();
        }

        class DependencyColumn : SourceGrid.Cells.Controllers.ControllerBase
        {
            private int mDependencyColumn;
            public DependencyColumn(int dependencyColumn)
            {
                mDependencyColumn = dependencyColumn;
            }

            public delegate object ConvertDelegate(object source);
            public ConvertDelegate ConvertFunction;

            public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e)
            {
                base.OnValueChanged(sender, e);

                //I use the OnValueChanged to link the value of 2 cells
                // changing the value of the other cell

                SourceGrid.Position otherCell = new SourceGrid.Position(sender.Position.Row, mDependencyColumn);
                SourceGrid.CellContext otherContext = new SourceGrid.CellContext(sender.Grid, otherCell);

                object newVal = sender.Value;
                if (ConvertFunction != null)
                    newVal = ConvertFunction(newVal);

                if (!object.Equals(otherContext.Value, newVal))
                    otherContext.Value = newVal;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值