English 中文(简体)
自定义 DataGridViewColumn(WinForms)中多个字段的自定义绑定
原标题:
  • 时间:2008-12-24 01:37:08
  •  标签:

I have a question regarding a data binding(of multiple properties) for custom DataGridViewColumn. Here is a schema of what controls that I have, and I need to make it bindable with DataGridView datasource. Any ideas or a link to an article discussing the matter?

控制

  • Graph Control(custom): Displayed in the custrom DataGridView column. Has properties like "Start Date", "EndDate", Windows Chart control, which is itself, bindable, etc.
  • Custom cell(DataGridViewCustomCell inherits from DataGridViewCell) that holds the Graph control and processes some events(OnEnter event, for example, passes the focus to the custom Graph column for drag-n-drop type of events, etc.)
  • Custom column(DataGridViewCustomColumn inherits from DataGridViewColumn) that defined the cell template type: CellTemplate = new DataGridViewCustomCell(); and also a primary choice for data binding

数据结构:

  • Main table to be displayed in other DataGridView Columns
  • Graph table - related to the Main table via parent-child relationship. Holds graph data
  • Chart table related to the graph table via parent-child relationship. Holds data for the win-form chart, which is a part of my Graph control.

直到目前为止,我甚至不能将Graph表中的数据绑定到我的Graph控件或Graph-holding列/单元格。

问题回答

感谢您的回答。我的数据来源不是SQL数据源,事实上,我在谈论Windows表单的数据网格视图(我不确定这是否清楚)。

由于我在任何一个问问题的论坛上都没有得到回复,我认为我会概述一下我想出来的解决方案,供那些可能遇到类似问题的人和可能的评论。 :-)

(steps 1-2 are also explained in the famous MS example) 1. Create your own classes that inherit from DataGridViewColumn and DataGridViewCell, setup the column template; 2. Create your "CustomEdit" control

  1. In the data item, whatever that is, a DataRow, or a List item, add a read-only property, that return the object itself. This property is bound to the custom column.

自定义单元格:

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;
        }
    }
 }

请看我的问题 这里

这很容易做,你只需要不使用IDE,而是全部用代码实现。虽然是很多工作,但如果你知道该怎么做,它并不难。我从一无所知到能够在一天内完成它,所以我相信你也能做到。

编辑:您还可以在用于填充DataGridView的SQL中使用Join。





相关问题
热门标签