有一个DataGridView(只读)和数据源,有些项是boolean。
DataGridView渲染这些项目的常用复选框,我想使用一些漂亮的刻度图像。我可以用自定义图像替换默认复选框渲染吗?
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
int cellsCount = dataGridView.Rows[i].Cells.Count;
for (int j = 0; j < cellsCount; j++)
{
var cells = dataGridView.Rows[i].Cells;
object value = cells[j].Value;
if (value is bool)
{
bool b = (bool)value;
if (b)
{
cells[j].Style.BackColor = Color.LightGreen;
//TODO - add some pretty image of tick
}
else
{
cells[j].Style.BackColor = Color.Orange;
//TODO - add some pretty image of red cross (cancel icon)
}
}
}
}
答案 0 :(得分:2)
我在之前的解决方案中使用了一些操作:
private void grdServices_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridViewImageColumn img = new DataGridViewImageColumn();
img.Name = "img";
img.HeaderText = "CheckOut";
img.ReadOnly = true;
int number_of_rows = grdServices.RowCount;
if (number_of_rows > 0)
{
grdServices.Columns.Add(img);
for (int i = 0; i < number_of_rows; i++)
{
if (bool.Parse(grdServices.Rows[i].Cells[27].Value.ToString()) == true)
{
grdServices.Rows[i].Cells["img"].Value = (System.Drawing.Image)Properties.Resources.yes;
}
else
{
grdServices.Rows[i].Cells["img"].Value = (System.Drawing.Image)Properties.Resources.no;
}
}
}
}
}
答案 1 :(得分:1)
你试过把图像放到单元格值吗?
dataGridView1.Rows[i].Cells[j].Value=new Bitmap("tickImg.png");