C# 如何使用htmlColor设置DataGridViewButtonCell背景色?
两天以来我一直在努力解决这个问题 我有一个XML文件,其中包含作为htmlColor代码的颜色,在我的程序中,我有一个DataGridView,它向我显示hexCodes中的值,我可以单击它并使用ColorDialog更改颜色值,然后将单元格背景色设置为所选颜色,并将hexCode作为新值返回给我 对不起,我不能发布照片,因为我没有10个声誉(我是新的) 我想要的是,当我在程序中打开XML文件时,单元格的背景颜色应该与单元格中写入的内容相同 我试过这个,但不起作用:(C# 如何使用htmlColor设置DataGridViewButtonCell背景色?,c#,xml,winforms,datagridview,datagridviewcellstyle,C#,Xml,Winforms,Datagridview,Datagridviewcellstyle,两天以来我一直在努力解决这个问题 我有一个XML文件,其中包含作为htmlColor代码的颜色,在我的程序中,我有一个DataGridView,它向我显示hexCodes中的值,我可以单击它并使用ColorDialog更改颜色值,然后将单元格背景色设置为所选颜色,并将hexCode作为新值返回给我 对不起,我不能发布照片,因为我没有10个声誉(我是新的) 我想要的是,当我在程序中打开XML文件时,单元格的背景颜色应该与单元格中写入的内容相同 我试过这个,但不起作用:( private void
private void dgvColors\u单元格格式(对象发送方,
DataGridViewCellFormattingEventArgs(e)
{
主题=新主题();
foreach(主题中的KeyValuePair颜色。颜色)
dgvColors.Columns[“colKey”]。DefaultCellStyle.BackColor=
ColorTranslator.FromHtml(color.Value.ToString());
}
将FlatStyle
设置为Flat
后,您可以更改每个单元格的背景色。使用Normal
样式,您只能在按钮周围看到一个1像素宽的边框
此示例在加载DGV后创建DataGridViewButtonCells
并绘制这些单元格:
for (int r = 0; r < DGV.Rows.Count; r++)
{
DGV[4, r] = new DataGridViewButtonCell();
((DataGridViewButtonCell)DGV[4, r]).Style.BackColor = Color.OrangeRed;
((DataGridViewButtonCell)DGV[4, r]).FlatStyle = FlatStyle.Flat;
((DataGridViewButtonCell)DGV[4, r]).Value = r + "RR";
}
for(int r=0;r
如果十六进制值正确,代码应该是可修改的。所以问题出在ColorTranslator()
它适用于此版本的代码:
private void dgvMenuColors_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dgvMenuColors.Rows)
{
row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml(row.Cells[1].Value.ToString());
}
}
:D它已经是平面样式,问题在于颜色转换器;)
private void dgvMenuColors_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dgvMenuColors.Rows)
{
row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml(row.Cells[1].Value.ToString());
}
}