Customize DataGridViewButtonColumn

G

Guest

Hi there,

I'd like to customize the DataGridViewButtonColumn to display a certain
button caption and having a value different from this. In general the DGVBC
shwos the assigned value as button caption.
I tried the whole thing by defining a custom DataGridViewColumn that has a
custom DataGridViewColumnCell class as its CellTemplate.
In the cell class I overwrite the Paint() method and reset the formattedValue.
I made this by adding properties to each class that set the buttons caption
and I also adjusted the clone() method to clone with the property.
But unfortunatly I can set the property to the classes but the property's
value is never set....

Why?

the DataGridViewColumnCell class

internal class CustomDataGridViewButtonColumnCell : DataGridViewButtonCell
{
private string _CustomText;

public string CustomText
{
get { return _CustomText; }
set { _CustomText = value; }
}

public override object Clone()
{
CustomDataGridViewButtonColumnCell cell =
(CustomDataGridViewButtonColumnCell)base.Clone();
cell.CustomText = this.CustomText;
return cell;
}

protected override void Paint(
Graphics graphics,
Rectangle clipBounds,
Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates cellState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
value, this.CustomText, errorText, cellStyle,
advancedBorderStyle, paintParts);
}
}

the DataGridViewColumn class:

internal class CustomDataGridViewButtonColumn : DataGridViewColumn
{
private string _CustomText;

public string CustomText
{
get { return _CustomText; }
set { _CustomText = value; }
}

public CustomDataGridViewButtonColumn()
{
CustomDataGridViewButtonColumnCell cell = new
CustomDataGridViewButtonColumnCell();
cell.CustomText = this.CustomText;
this.CellTemplate = cell;
}

public override object Clone()
{
CustomDataGridViewButtonColumn column =
(CustomDataGridViewButtonColumn)base.Clone();
column.CustomText = this.CustomText;
return column;
}
}


the adding of the column to the DataGridView:

CustomDataGridViewButtonColumn col = new CustomDataGridViewButtonColumn();
col.MinimumWidth = col.Width = 60;
col.DataPropertyName = "pluginKey";
col.Name = col.HeaderText = "Remove";
col.CustomText = "Remove";
dataGridViewPlugins.Columns.Add(col);


Thanks for any help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top