C# Winform 单个单元格中的多个图像 DataGirdViewImageColumn [英] C# Winform multiple image in a single cell DataGirdViewImageColumn
问题描述
我想在 DataGirdViewImageColumn 的一个单元格中添加多张图片
I want add multiple image into one cell in DataGirdViewImageColumn
你可以很容易地看到五个不同颜色的圆圈,起初,我试图在 DataGirdViewTextBoxColumn 中写•"字符,但我无法为每个字符更改颜色,有人知道这样做吗?
You can easily seen five circles with different color, at first, I've tried to written "•" character in DataGirdViewTextBoxColumn but I can not change color for each character, does anyone have any idea to do this?
非常感谢
推荐答案
下面是一个单元格绘制这个单元格的例子:
Here is an example of cell-painting this one Cell:
CellPainting 事件完成工作:
The CellPainting event does the work:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2 && e.RowIndex >= 0 && e.Value != null)
{
// use your own code here...
string val = ((int)e.Value).ToString("00000000");
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.PaintBackground(e.CellBounds, false);
for (int i = 0; i < count; i++)
using (SolidBrush brush = new // ..and here!!!
SolidBrush(colors[Convert.ToInt16(val[i].ToString())]))
e.Graphics.FillEllipse(brush,
e.CellBounds.X + i * 12 + 6, e.CellBounds.Y + 5 , 11, 11);
e.Handled = true;
}
}
它使用预定义的颜色列表:
It makes use of a predefined list of colors:
List<Color> colors = new List<Color>() // use your own set of colors here!
{ Color.Red, Color.Black, Color.Blue, Color.ForestGreen, Color.DarkKhaki,
Color.Goldenrod, Color.DeepPink, Color.Orange, Color.DarkSlateGray, Color.GreenYellow };
以及要绘制多少点的计数:
And a count of how many dots to draw:
int count = 7; // ditto!
显然,您可能想要更改我存储测试数据的方式...
Obviously you may want to change the way I stored the data for my test...
我存储了一个大整数作为单元格的值,每个数字都映射到一种颜色上.
I have stored a large integer number as the cell's value and each digint is mapped onto one color.
你可以(并且可能应该)改变这个!
you can (and probably should) change this!
以下是我设置 DGV 的方式:
Here is how I set up may DGV:
dataGridView1.Columns.Add("asd", "asd");
dataGridView1.Columns.Add("ko", "ok");
dataGridView1.Columns.Add("col", "col");
dataGridView1.Rows.Add(66);
for (int r = 0; r < 66; r++)
{
dataGridView1[0, r].Value = r;
dataGridView1[1, r].Value = r * 3.14f;
dataGridView1[2, r].Value = (int )( r * 314.345 + 1231542f);
}
显然没有你会怎么做;-)
这篇关于C# Winform 单个单元格中的多个图像 DataGirdViewImageColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!