Jelly Jelly - 3 months ago 30
C# Question

custom datagridview cell?

I'm doing a project where I need to add a field (let's called it field) to DataGridView cells. The point of this is to be able an extra field in DataGriidView cells which will make the rest of the project far easier.

I've created the following:

public class CustomGridRow:DataGridRow{}
public class CustomGridColumn:DataGridViewColumn
{
public CustomGridColumn
{
This.TemplateCell = new CustomGridTextBoxCell()
}
}
public class CustomGridTextBoxCell: DataGridViewTextBoxCell
{
public string field;
}


Problem:
If I create a class like (which is what I want to achieve):

public class CustomGridCell: DataGridViewCell{}


And move the field to
CustomGridCell
, I would want the
CustomGridTextBoxCell
to inherit from the new
CustomGridCell
, however it does already have a base class of
DataGridViewCell
and C# does not allow a class to inherit two bases classes.

My understanding is the solution comes through
Interfaces
? Any idea how to fix?

Answer

Try

class CustomDataGridColumn : DataGridViewColumn
{
     this.CellTemplate = new CustomGridTextBoxCell();
}

class CustomGridTextBoxCell : CustomGridCell
{

}

class CustomGridCell : DataGridViewCell
{
    public string fieldA { get; set; }

    public CustomGridCell()
    {

    }
}
Comments