gpt4 book ai didi

c# - 使用 DataGridViewComboBoxColumn 在 DataGridView 中显示所选图像?

转载 作者:行者123 更新时间:2023-11-30 13:04:25 25 4
gpt4 key购买 nike

我在使用 DataGridView 和 DataGridViewComboBoxColumn 让用户从图像列表中选择图像时遇到问题。在标题为“DatagridViewComboBoxColumn 的自定义绘制”的问题中进行讨论后 ref Link .我也面临这个问题,因为只有当单元格处于编辑模式时才会绘制图像。当我点击组合框单元格外的某处时,所选图像将消失!我已经实现了 CellPainting 事件来重绘图像,但仍然无法解决问题。我使用以下代码测试了 DataGridViewComboBoxColumn:

    public Form1()
{
InitializeComponent();
.....
imageList.Images.Add(Properties.Resources.icon_priority_low);
imageList.Images.Add(Properties.Resources.icon_priority_medium);
.....
}

private void Form1_Load(object sender, EventArgs e)
{
.....
DataGridViewComboBoxCell dgvcbc = (DataGridViewComboBoxCell)newDataGridView1.Rows[0].Cells[1];
dgvcbc.Items.Add("test0");
dgvcbc.Items.Add("test1");
.....
}

private void newDataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox)
{
ComboBox theCB = (ComboBox)e.Control;
theCB.DrawMode = DrawMode.OwnerDrawFixed;
try
{
theCB.DrawItem -= combobox1_DrawItem;
}
catch { }
theCB.DrawItem += combobox1_DrawItem;
}
}

private void combobox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Brush br = SystemBrushes.WindowText;
Brush brBack;
Rectangle rDraw;
bool bSelected = e.State == DrawItemState.Selected;
bool bValue = e.State == DrawItemState.ComboBoxEdit;

if ((e.Index < 0) || (columnIndex != 1))
return;

rDraw = e.Bounds;
rDraw.Inflate(-1, -1);

int x, y;

x = e.Bounds.Left + 25;
y = e.Bounds.Top + 1;
int midX = (int)(e.Bounds.Width / 2) + e.Bounds.Left;

// Show image and ignore text.
g.DrawImage(imageList.Images[e.Index], new Rectangle(midX - 6, y + 2, 12, 12));
}

private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (columnIndex != 1)
return;

Graphics g = e.Graphics;
Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true);

e.PaintBackground(e.ClipBounds, true);
e.PaintContent(e.ClipBounds);

using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
int y = rDraw.Y + 1;
int midX = (int)(rDraw.Width / 2) + rDraw.X;

g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12));

e.PaintContent(e.ClipBounds);
e.Handled = true;
}
}
}

如果我单击 DataGridView 的其他单元格,单元格将显示“test0”而不是 Images[0]。你能帮忙解决这个问题吗?非常感谢。

最佳答案

最后一次调用 PaintContent() 会删除您绘制的图像。

在绘制图像之前,您必须绘制单元格(而不是前景)。它看起来像这样:

private void newDataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (columnIndex != 1)
return;

Graphics g = e.Graphics;
Rectangle rDraw = newDataGridView1.GetCellDisplayRectangle(columnIndex, rowIndex, true);

e.Paint(e.CellBounds, e.PaintParts & ~DataGridViewPaintParts.ContentForeground);

int y = rDraw.Y + 1;
int midX = (int)(rDraw.Width / 2) + rDraw.X;

g.DrawImage(imageList.Images[0], new Rectangle(midX - 6, y + 2, 12, 12));

e.Handled = true;
}

关于c# - 使用 DataGridViewComboBoxColumn 在 DataGridView 中显示所选图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com