Telerik Radgridview Winforms 更改单元格/行背景颜色

use*_*364 2 c# telerik-grid

我只是想运行从表单上的按钮事件触发的验证检查。对于网格内的每一行,它检查第一个单元格以查看该值是否与字符串匹配。对于每一个失败的人,我都希望它将单元格或行背景颜色更改为红色。当用户更正单元格值并再次点击按钮时,它将反转回默认颜色。对于我的生活,我无法让它改变颜色。我知道有一个单元格格式事件,但我肯定不必为了改变单元格/行的样式而经历一个事件。我正在尝试以下代码而没有任何运气。我见过的所有例子都是基于事件的,所以希望我可以完全避免这种情况。

if (!ValidateParametersExistInScript(rtxtQuery.Text))
{
    GridViewCellInfo cell = rgvNewParameters.Rows[0].Cells["ParameterName"];
    cell.Style.BackColor = Color.Red;
    cell.Style.DrawFill = true;
    MessageBox.Show("Parameters do not match definition inside query, " + 
                    "please check spelling and try again.");
}
Run Code Online (Sandbox Code Playgroud)

Sil*_*gel 6

您必须先将 CustomizeFill 设置为 true,然后才能修改 GridViewCellInfo 的背景颜色。

if (!ValidateParametersExistInScript(rtxtQuery.Text))
{
    GridViewCellInfo cell = rgvNewParameters.Rows[0].Cells["ParameterName"];
    cell.Style.CustomizeFill = true;
    cell.Style.BackColor = Color.Red;
    cell.Style.DrawFill = true;
    MessageBox.Show("Parameters do not match definition inside query, " + 
                    "please check spelling and try again.");
}
Run Code Online (Sandbox Code Playgroud)