如何在 DataGridViewCell 内绘制自定义 ComboBox?
我有一个自定义的 ComboBox
控件,我想在 DataGridViewCell
中使用它。我首先继承了DataGridViewCell
,并尝试重写Paint()
方法来绘制单元格中的ComboBox
。
我的问题是,继承 DataGridViewColumn
并将 CellTemplate
属性设置为我的 CustomDataGridViewCell
类的新实例后,单元格为灰色,没有内容。
cBox
类变量在对象构造函数中实例化。
protected override void Paint(Graphics graphics, Rectangle clipBounds,
Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle borderStyle,
DataGridViewPaintParts paintParts)
{
// Call MyBase.Paint() without passing object for formattedValue param
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
"", errorText, cellStyle, borderStyle, paintParts);
// Set ComboBox properties
this.cBox.CheckOnClick = true;
this.cBox.DrawMode = System.Windows.Forms.DrawMode.Normal;
this.cBox.DropDownHeight = 1;
this.cBox.IntegralHeight = false;
this.cBox.Location = new System.Drawing.Point(cellBounds.X, cellBounds.Y);
this.cBox.Size = new System.Drawing.Size(cellBounds.Width, cellBounds.Height);
this.cBox.ValueSeparator = ", ";
this.cBox.Visible = true;
this.cBox.Show();
}
如何在单元格中正确绘制ComboBox
?
I have a custom ComboBox
control that I want to use in a DataGridViewCell
. I first inherited DataGridViewCell
and am trying to override the Paint()
method to paint the ComboBox
in the cell.
My problem is that after inheriting DataGridViewColumn
and setting the CellTemplate
property to a new instance of my CustomDataGridViewCell
class, the cell is grey with no contents.
The cBox
class variable is instantiated in the object ctor.
protected override void Paint(Graphics graphics, Rectangle clipBounds,
Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle borderStyle,
DataGridViewPaintParts paintParts)
{
// Call MyBase.Paint() without passing object for formattedValue param
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
"", errorText, cellStyle, borderStyle, paintParts);
// Set ComboBox properties
this.cBox.CheckOnClick = true;
this.cBox.DrawMode = System.Windows.Forms.DrawMode.Normal;
this.cBox.DropDownHeight = 1;
this.cBox.IntegralHeight = false;
this.cBox.Location = new System.Drawing.Point(cellBounds.X, cellBounds.Y);
this.cBox.Size = new System.Drawing.Size(cellBounds.Width, cellBounds.Height);
this.cBox.ValueSeparator = ", ";
this.cBox.Visible = true;
this.cBox.Show();
}
How can I correctly paint the ComboBox
in the cell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了相当简单的更改来解决我的问题。
我必须将坐标更正为相对于窗口而不是 DataGridView,为所属表单调用 Controls.Add() 并将控件重新定位在DataGridView:
I made fairly simple change that fixes my problem.
I had to correct the coordinates to be relative to the window instead of the
DataGridView
, callControls.Add()
for the owning form, and reposition the control in front of theDataGridView
: