自定义datagridview单元格?
我正在做一个项目,我需要添加一个字段(我们称之为字段)到DataGridView单元格。这一点是为了能够在DataGriidView单元格中添加一个额外的字段,这将使项目的其余部分更加容易。自定义datagridview单元格?
我创建了以下内容:
public class CustomGridRow:DataGridRow{}
public class CustomGridColumn:DataGridViewColumn
{
public CustomGridColumn
{
This.TemplateCell = new CustomGridTextBoxCell()
}
}
public class CustomGridTextBoxCell: DataGridViewTextBoxCell
{
public string field;
}
问题: 如果我创建像一个类(这是我想达到的目标):
public class CustomGridCell: DataGridViewCell{}
而且场移到CustomGridCell
,我希望CustomGridTextBoxCell
继承自新的CustomGridCell
,但它已经有一个基类DataGridViewCell
和C#不允许类继承两个基类。
我的理解是通过Interfaces
来解决吗?任何想法如何解决?
尝试
class CustomDataGridColumn : DataGridViewColumn
{
this.CellTemplate = new CustomGridTextBoxCell();
}
class CustomGridTextBoxCell : CustomGridCell
{
}
class CustomGridCell : DataGridViewCell
{
public string fieldA { get; set; }
public CustomGridCell()
{
}
}
假设我明白你想要做什么,我想你可以这样做:
class CustomGridTextBoxCell : CustomGridCell
{
public CustomGridTextBoxCell(string field)
: base(field)
{
}
}
abstract class CustomGridCell : DataGridViewCell
{
private string _field;
public CustomGridCell(string field)
{
this._field = field;
}
public string field
{
get { return this._field; }
}
}
注:我没有测试这一点,它只是十首发。
UPDATE:怎么样,如果你改变了抽象类是这样的,那么:
abstract class CustomGridCell : DataGridViewCell
{
public string field { get; set; };
public CustomGridCell(string field)
{
this.field = field;
}
}
UPDATE
您也可以尝试:
class CustomDataGridColumn : DataGridViewColumn
{
this.CellTemplate = new CustomGridTextBoxCell();
}
class CustomGridTextBoxCell : CustomGridCell
{
}
class CustomGridCell : DataGridViewCell
{
public string fieldA { get; set; }
public CustomGridCell()
{
}
}
当我回家并告诉你时,会测试它。 –
似乎工作...我想实现的是创建一个自定义的datagridview其datagridviewcells有一个字段 –
你的意思是'不'似乎工作? – sr28
难道你不能使用抽象类,然后创建一个继承自它的具体类吗? – sr28
你能说明一个答案吗? @ sr28 –