我希望在同一个datagridview列中有两个控件。
我想自定义DataGridViewComboBoxCell,以便它显示所选值的值,并在其上显示带有一些文本的浮动标签。在过去,我能够使用复选框和标签来完成它,但DataGridViewComboBoxCell的问题是,当我覆盖绘制事件时,它会出现一个空的数据源。
我在使用Paint事件后尝试再次分配数据源,但是当我看到DataGridViewComboBoxCell中的值和显示正确值的标签时,我进入无限循环,所以我看到GUI不断闪烁。
10x的帮助。
代码如下:
*表单加载时
MyDGVCheckBoxColumn col = new MyDGVCheckBoxColumn();
col.DataPropertyName = "value";
col.DataSource = list;
col.DisplayMember = "Yes";
col.ValueMember = "value";
col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
this.dataGridView1.Columns.Add(col);
this.dataGridView1.RowCount = 50;
通用列表的类:
public class CheckThis
{
public string Yes { get; set; }
public string value { get; set; }
public CheckThis()
{
Yes = "gggg";
value = "1";
}
}
自定义DataGridViewComboBoxCell的代码(我过去从某个站点使用过类似的例子)
public class MyDGVCheckBoxColumn : DataGridViewComboBoxColumn
{
private string label;
public string Label
{
get
{
return label;
}
set
{
label = value;
}
}
public override DataGridViewCell CellTemplate
{
get
{
return new MyDGVCheckBoxCell();
}
}
}
public class MyDGVCheckBoxCell : DataGridViewComboBoxCell
{
private string label;
public string Label
{
get
{
return label;
}
set
{
label = value;
}
}
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)
{
// the base Paint implementation paints the check box
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
// Get the check box bounds: they are the content bounds
Rectangle contentBounds = this.GetContentBounds(rowIndex);
// Compute the location where we want to paint the string.
Point stringLocation = new Point();
stringLocation.Y = cellBounds.Y + 30;
stringLocation.X = cellBounds.X + contentBounds.Bottom;
// Paint the string.
var res = false;
MyDGVCheckBoxColumn col = (MyDGVCheckBoxColumn)this.OwningColumn;
col.DataSource = list;
col.DisplayMember = "Yes";
col.ValueMember = "value";
this.label = "Customer Does Not Appear";
graphics.DrawString(
this.Label, new Font("Arial", 6, FontStyle.Bold), System.Drawing.Brushes.Red, stringLocation);
}
public object list { get; set; }
}