Custom DataGridView列(WinForms)中多个字段的自定义绑定

时间:2008-12-24 01:37:09

标签: data-binding datagridview datagridviewcolumn

我对自定义DataGridViewColumn的数据绑定(多个属性)有疑问。 这是我拥有的控件的架构,我需要使它与DataGridView数据源绑定。任何想法或链接到讨论此事的文章?

控制

  • 图形控件(自定义):显示在 custrom DataGridView列。具有 像“开始日期”这样的属性, “EndDate”,Windows Chart控件, 这本身就是可绑定的等等。
  • 自定义单元格(DataGridViewCustomCell继承 来自DataGridViewCell) Graph控制并处理一些 事件(OnEnter事件,例如, 将焦点传递给自定义图表 drag-n-drop类型的列 事件等。)
  • 自定义列(DataGridViewCustomColumn 继承自DataGridViewColumn) 定义了单元格模板类型: CellTemplate = new DataGridViewCustomCell();还有一个 数据绑定的主要选择

数据结构:

  • 要在其他DataGridView列中显示的主表
  • 图表 - 通过父子关系与主表相关。保存图表数据
  • 通过父子关系与图表相关的图表。保存win-form图表的数据,这是我的Graph控件的一部分。

到目前为止,我甚至无法将Graph表中的数据绑定到Graph控件或Graph-holding Column / Cell。

2 个答案:

答案 0 :(得分:2)

感谢您的回答。我的数据源不是SQL数据源,事实上我在谈论win-forms的datagridview(我不确定这是否清楚)。

由于我没有得到任何论坛的回复,我提出这个问题,我想,我会概述一个我想出的解决方案,对于那些可能有类似问题和可能的批评的人。 :-)

(步骤1-2也在着名的MS示例中解释) 1.创建自己的继承自DataGridViewColumn和DataGridViewCell的类,设置列模板; 2.创建“CustomEdit”控件

  1. 在数据项中,无论是DataRow还是List项,都添加一个只读属性,它返回对象本身。此属性绑定到自定义列。
  2. 自定义单元格:

    public partial class MyCell : DataGridViewCell 
     {
        protected override void Paint(...)
            {...} // draws control
                  // receives data item as a value 
                  // in my case I have to custom-draw entire control in this fnc.
        public override void InitializeEditingControl(...)
            {...} // initialize control editing
        // override some other properties
        public override Type EditType {
            get{ 
               return typeof(MyEditControl);
            }
        }
        public override Type ValueType{
            get{
               return typeof(MyItem);
            }
        }
     }
    

    自定义列:

    public partial class MyColumn : DataGridViewColumn
    {
        public MyColumn(){ ...
        CellTemplate = new MyCell();
        }
    }
    

    编辑控件:

    public partial class MyEditControl : UserControl, IDataGridViewEditingControl
    {... // implements IDataGridViewEditingControl
         // value is our data item
    }
    

    数据项,数据源变为List< MyItem>

    public class MyItem:Object{
        ...
        [XmlIgnore] // I need it because I do serialization
        public MyItem Self {
            get {
                return this;
            }
        }
     }
    

答案 1 :(得分:0)

请参阅我的问题Here

这很容易做到,你只是不使用IDE来做,你在代码中完成所有操作。这是很多工作,但如果你知道你在做什么,那并不难。我不知道什么都不知道能在不到一天的时间里做到这一点,所以我相信你能够做到。

编辑:你也可以在填充datagridview

的sql中使用Join