Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用htmlColor设置DataGridViewButtonCell背景色?_C#_Xml_Winforms_Datagridview_Datagridviewcellstyle - Fatal编程技术网

C# 如何使用htmlColor设置DataGridViewButtonCell背景色?

C# 如何使用htmlColor设置DataGridViewButtonCell背景色?,c#,xml,winforms,datagridview,datagridviewcellstyle,C#,Xml,Winforms,Datagridview,Datagridviewcellstyle,两天以来我一直在努力解决这个问题 我有一个XML文件,其中包含作为htmlColor代码的颜色,在我的程序中,我有一个DataGridView,它向我显示hexCodes中的值,我可以单击它并使用ColorDialog更改颜色值,然后将单元格背景色设置为所选颜色,并将hexCode作为新值返回给我 对不起,我不能发布照片,因为我没有10个声誉(我是新的) 我想要的是,当我在程序中打开XML文件时,单元格的背景颜色应该与单元格中写入的内容相同 我试过这个,但不起作用:( private void

两天以来我一直在努力解决这个问题

我有一个XML文件,其中包含作为htmlColor代码的颜色,在我的程序中,我有一个DataGridView,它向我显示hexCodes中的值,我可以单击它并使用ColorDialog更改颜色值,然后将单元格背景色设置为所选颜色,并将hexCode作为新值返回给我

对不起,我不能发布照片,因为我没有10个声誉(我是新的)

我想要的是,当我在程序中打开XML文件时,单元格的背景颜色应该与单元格中写入的内容相同

我试过这个,但不起作用:(

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()); 
} 
}