我正在尝试有一个自定义的DatagridViewCell,它有3个可水平单击的按钮。我已经尽我所能在如下所示的代码,但我需要一种方法来显示单元格中的3个按钮。我只能画文本到目前为止。我甚至尝试声明一个Panel对象,以防更容易地操作按钮。
public partial class CustomButtonCell : DataGridViewButtonCell
{
private Panel buttonPanel;
private Button editButton;
private Button deleteButton;
private Button approveButton;
private Button cancelButton;
public bool Enabled { get; set; }
public CustomButtonCell()
{
this.buttonPanel = new Panel();
this.editButton = new Button();
this.deleteButton = new Button();
this.approveButton = new Button();
this.cancelButton = new Button();
this.editButton.Text = "Edit";
this.deleteButton.Text = "Delete";
this.approveButton.Text = "Approve";
this.cancelButton.Text = "Cancel";
this.buttonPanel.Controls.Add(this.editButton);
this.buttonPanel.Controls.Add(this.deleteButton);
this.buttonPanel.Controls.Add(this.approveButton);
this.buttonPanel.Controls.Add(this.cancelButton);
this.Enabled = true;
}
// Override the Clone method so that the Enabled property is copied.
public override object Clone()
{
CustomButtonCell cell = (CustomButtonCell )base.Clone();
cell.Enabled = this.Enabled;
return cell;
}
protected override void Paint(Graphics graphics,
Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates elementState, object value,
object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// Call the base class method to paint the default cell appearance.
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState,
value, formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
// Calculate the area in which to draw the button.
Rectangle buttonArea1 = cellBounds;
Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);
buttonArea1.X += buttonAdjustment.X;
buttonArea1.Y += buttonAdjustment.Y;
buttonArea1.Height -= buttonAdjustment.Height;
buttonArea1.Width -= buttonAdjustment.Width;
Rectangle buttonArea2 = cellBounds;
Rectangle buttonAdjustment2 = this.BorderWidths(advancedBorderStyle);
buttonArea2.X += buttonAdjustment2.X + buttonArea1.Width;
buttonArea2.Y += buttonAdjustment2.Y;
buttonArea2.Height -= buttonAdjustment2.Height;
buttonArea2.Width -= buttonAdjustment2.Width;
Rectangle buttonArea3 = cellBounds;
Rectangle buttonAdjustment3 = this.BorderWidths(advancedBorderStyle);
buttonArea3.X += buttonAdjustment3.X + buttonArea2.Width;
buttonArea3.Y += buttonAdjustment3.Y;
buttonArea3.Height -= buttonAdjustment3.Height;
buttonArea3.Width -= buttonAdjustment3.Width;
Rectangle buttonArea4 = cellBounds;
Rectangle buttonAdjustment4 = this.BorderWidths(advancedBorderStyle);
buttonArea4.X += buttonAdjustment4.X + buttonArea3.Width;
buttonArea4.Y += buttonAdjustment4.Y;
buttonArea4.Height -= buttonAdjustment4.Height;
buttonArea4.Width -= buttonAdjustment4.Width;
// Draw the disabled button.
ButtonRenderer.DrawButton(graphics, buttonArea1, PushButtonState.Default);
ButtonRenderer.DrawButton(graphics, buttonArea2, PushButtonState.Default);
ButtonRenderer.DrawButton(graphics, buttonArea3, PushButtonState.Default);
ButtonRenderer.DrawButton(graphics, buttonArea4, PushButtonState.Default);
// Draw the disabled button text.
TextRenderer.DrawText(graphics, "Test", this.DataGridView.Font, buttonArea1, SystemColors.GrayText);
TextRenderer.DrawText(graphics, "Test", this.DataGridView.Font, buttonArea2, SystemColors.GrayText);
TextRenderer.DrawText(graphics, "Test", this.DataGridView.Font, buttonArea3, SystemColors.GrayText);
TextRenderer.DrawText(graphics, "Test", this.DataGridView.Font, buttonArea4, SystemColors.GrayText);
}
// Force the cell to repaint itself when the mouse pointer enters it.
protected override void OnMouseEnter(int rowIndex)
{
}
// Force the cell to repaint itself when the mouse pointer leaves it.
protected override void OnMouseLeave(int rowIndex)
{
}
}
public class CustomButtonColumn : DataGridViewColumn
{
public CustomButtonColumn()
{
this.CellTemplate = new CustomButtonCell ();
}
}
1条答案
按热度按时间z2acfund1#
我同意有时候有一个可靠的用例来显示
UserControl
,无论它是“三个按钮”还是每行都有自己的滚动Chart
真实的数据或其他!一个长期以来对我有效的方法,经过尝试和验证,是有一个类似于下面代码的DataGridViewUserControlColumn
类,它可以在单元格边界中托管一个控件,而不仅仅是绘制一个。操作原理是允许绑定数据类具有从
Control
派生的属性。DGV中的(s)可以被换出。然后,当DataGridViewUserControlCell
被“绘制”而不是绘制单元格时,所发生的是控件被移动(如有必要),使其边界与正在绘制的单元格边界一致。由于用户控件位于DataGridView.Controls集合中,UC保持在z顺序的顶部,并且与任何容器的任何子容器相同地绘制。项的UserControl在首次绘制时添加到
DataGridView.Controls
集合,并在单元格的DataGridView
属性设置为null时(例如,用户删除行时)移除。启用AllowUserToAddRows
选项时,“new row”列表项在项编辑完成之前不会显示控件。典型记录类
配置DGV
带画图覆盖的自定义单元格
自定义列