Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridViewTextBoxCell中的主机复选框_C#_Datagridview_Datagridviewtextboxcell - Fatal编程技术网

C# DataGridViewTextBoxCell中的主机复选框

C# DataGridViewTextBoxCell中的主机复选框,c#,datagridview,datagridviewtextboxcell,C#,Datagridview,Datagridviewtextboxcell,我有一个自定义DataGridView,其中包含许多不同的单元格类型,这些单元格类型继承自DataGridViewTextBoxCell和DataGridViewCheckBoxCell 每个自定义单元格都有一个用于设置背景颜色(网格的某些功能需要)的属性,称为CellColor 为简单起见,我们将使用两个自定义单元格: public class FormGridTextBoxCell : DataGridViewTextBoxCell { public Color CellColou

我有一个自定义DataGridView,其中包含许多不同的单元格类型,这些单元格类型继承自DataGridViewTextBoxCell和DataGridViewCheckBoxCell

每个自定义单元格都有一个用于设置背景颜色(网格的某些功能需要)的属性,称为CellColor

为简单起见,我们将使用两个自定义单元格:

public class FormGridTextBoxCell : DataGridViewTextBoxCell
{
     public Color CellColour { get; set; }
     ...
}

public class FormGridCheckBoxCell : DataGridViewCheckBoxCell
{
     public Color CellColour { get; set; }
     ...
}
问题:

这意味着每当我想为包含FormGridTextBoxColumn和FormGridCheckBoxColumn类型的列(分别具有上述自定义单元格类型的CellTemplates)的网格行设置CellColor属性时,我必须执行以下操作:

if(CellToChange is FormGridTextBoxCell)
{
     ((FormGridTextBoxCell)CellToChange).CellColour = Color.Red;
}
else if (CellToChange is FormGridCheckBoxCell)
{
     ((FormGridCheckBoxCell)CellToChange).CellColour = Color.Red;
}
if(CellToChange is FormGridCell)
{
     ((FormGridCell)CellToChange).CellColour = Color.Red;
     ...
}
当你有3+种不同的细胞类型时,这将变得非常困难,我相信有更好的方法可以做到这一点

寻求的解决办法:

我的想法是,如果我可以创建一个从DataGridViewTextBoxCell继承的类,然后让自定义单元格类型从该类继承:

public class FormGridCell : DataGridViewTextBoxCell
{
     public Color CellColour { get; set }
}

public class FormGridTextBoxCell : FormGridCell
{
     ...
}

public class FormGridCheckBoxCell : FormGridCell
{
     ...
}
然后,我只需要执行以下操作:

if(CellToChange is FormGridTextBoxCell)
{
     ((FormGridTextBoxCell)CellToChange).CellColour = Color.Red;
}
else if (CellToChange is FormGridCheckBoxCell)
{
     ((FormGridCheckBoxCell)CellToChange).CellColour = Color.Red;
}
if(CellToChange is FormGridCell)
{
     ((FormGridCell)CellToChange).CellColour = Color.Red;
     ...
}
不管有多少自定义单元格类型(因为它们都将从FormGridCell继承);任何特定的控件驱动单元类型都将在其中实现Windows窗体控件

为什么这是一个问题:

我尝试了以下这篇文章:

这适用于自定义日期时间选择器,但是在DataGridViewTextBoxCell中托管复选框是另一回事,因为有不同的属性来控制单元格的值


如果有一种更简单的方法可以从DataGridViewTextBoxCell开始,并将继承类中的数据类型更改为预定义的数据类型,那么我愿意接受建议,但是核心问题是在DataGridViewTextBoxCell中托管一个复选框。

我相信您已经发现,单个类只能从一个基类继承。您需要的是一个
界面
,例如:

public interface FormGridCell
{
    Color CellColor { get; set; }
}
从那里,您可以非常类似地创建子类,从它们各自的
DataGridViewCell
类型继承,并实现
接口

public class FormGridTextBoxCell : DataGridViewTextBoxCell, FormGridCell
{
    public Color CellColor { get; set; }
}

public class FormGridCheckBoxCell : DataGridViewCheckBoxCell, FormGridCell
{
    public Color CellColor { get; set; }
}
在这一点上,用法就像你希望的那样简单;通过
CellTemplate
创建单元格,并根据需要将单元格强制转换为
界面
类型,您可以自由地执行所需操作(为了直观地查看结果,我设置了单元格的背景色作为示例):


那正是我想要的。非常感谢!是的,问题的根源主要是因为只能从单个基类继承。