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

时间:2014-06-10 19:05:42

标签: c# telerik-grid

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

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.");
}

2 个答案:

答案 0 :(得分:3)

必须先将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.");
}

答案 1 :(得分:1)

 private void radGridLedgerAccount_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.RowIndex == 0  || e.RowIndex==radGridLedgerAccount.Rows.Count-1)
            {

                e.CellElement.DrawFill = true;
                e.CellElement.ForeColor = Color.White;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.BackColor = Color.Red;
            }
            else
            {
                if (e.CellElement.ColumnInfo.Name == "months") //checking for the text in header cell
                {
                    if (e.RowIndex != 0)
                    {
                        System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#6599FF");
                        e.CellElement.DrawFill = true;
                        e.CellElement.ForeColor = Color.White;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = col;
                    }
                }

                else
                {
                    e.CellElement.DrawFill = true;
                    e.CellElement.ForeColor = Color.Black;
                    e.CellElement.NumberOfColors = 1;
                    e.CellElement.BackColor = Color.White;
                    e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
                }

            }
相关问题