我的DataGridView
上有一个DataGridViewComboBoxColumn
。这是我的自定义单元格的绘画处理程序:自动调整使用自定义绘画的DataGridViewComboBoxCell
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex >= 0)
{
e.PaintBackground(e.CellBounds, true);
//e.PaintContent(e.CellBounds);
Graphics g = e.Graphics;
Color c = Color.Empty;
string s = "";
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
rDraw = e.CellBounds;
rDraw = Rectangle.FromLTRB(e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Bottom - 1);
brBack = Brushes.White;
Pen penGridlines = new Pen(dataGridView.GridColor);
g.DrawRectangle(penGridlines, rDraw);
g.FillRectangle(brBack, rDraw);
penGridlines.Dispose();
if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
ComboboxColourItem oColourItem = (ComboboxColourItem)dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
s = oColourItem.ToString();
c = oColourItem.Value;
}
int butSize = e.CellBounds.Height;
Rectangle rbut = new Rectangle(e.CellBounds.Right - butSize,
e.CellBounds.Top, butSize, butSize);
ComboBoxRenderer.DrawDropDownButton(e.Graphics, rbut,
System.Windows.Forms.VisualStyles.ComboBoxState.Normal);
if (c != Color.Empty)
{
SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle(e.CellBounds.Left + 6,
e.CellBounds.Top + 5, 10, 10);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, Form.DefaultFont, Brushes.Black,
e.CellBounds.Left + 25, e.CellBounds.Top + 3);
b.Dispose();
}
e.Handled = true;
}
}
当我去了,以自动调整我的填充柱双击DVG这个右边的编辑是发生了什么:
我如何调整行为,以便在自动调整时考虑组合下拉菜单?
谢谢。
那是油漆之外众所周知,下拉按钮的宽度事件? – DonBoitnott
@DonBoitnott不,但宽度在paint事件中指定为'e.CellBounds.Height',所以也许我们有它? –
我不使用DGV,所以我不知道可能有哪些事件可用,但我会查找(按顺序):直接覆盖列自动大小;调整大小后会发生的事情;或'ColumnWidthChanged'。您应该可以使用其中的一个来确定它是否是您关心的列,并为按钮添加更多宽度。 AutoSize永远不会知道它,所以你必须手工完成。 – DonBoitnott