当我点击选择它们时,如何更改Devexpress Grid中单元格的背景颜色?

时间:2015-01-24 09:04:44

标签: c# devexpress xtragrid

我有一个包含7列的DevExpress XtraGrid

我想在单击选择时更改单元格背景颜色。我尝试GridViewInfo,但它只占用屏幕上可见的列。但我想为所有专栏做。 (不是RowCellStyle)你有解决方案吗?

private static DataTable CreateTable(int RowCount)
{
    DataTable tbl = new DataTable();
    tbl.Columns.Add("Question", typeof(string));
    tbl.Columns.Add("A", typeof(string));
    tbl.Columns.Add("B", typeof(string));
    tbl.Columns.Add("C", typeof(string));
    tbl.Columns.Add("D", typeof(string));
    tbl.Columns.Add("E", typeof(string));
    for (int i = 0; i < RowCount; i++)
        tbl.Rows.Add(new object[] { String.Format("Question: {0}", i), "", "", "", "", "" });
    return tbl;
}

public Form1()
{
    InitializeComponent();
    gridControl1.DataSource = CreateTable(5);
}

private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
{
    GridView View = sender as GridView;
    if (e.Column.FieldName == "A" || e.Column.FieldName == "B" || e.Column.FieldName == "C" || e.Column.FieldName == "D" || e.Column.FieldName == "E")
    {
        if (e.RowHandle >= 0)
        { 
            e.Appearance.BackColor = Color.Green;
            //gridView1.Appearance.FocusedCell.BackColor = Color.Green;
        } 
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要处理GridView.ShownEditor事件。

private void GrdView_ShownEditor(object sender, EventArgs e)
{
  GridView view = (GridView)sender;
  GrdView.FocusedRowHandle = view.FocusedRowHandle;
  GrdView.FocusedColumn = GrdView.FocusedColumn;

  GrdView.ShowEditor();

  dynamic editor = GrdView.ActiveEditor;

  //editor.Enabled = False

  editor.BackColor = Color.Blue;

  //view.Invalidate()
}

但是只要您将鼠标悬停在其他单元格上,它就会将单元格恢复到最后一个状态。在这种情况下,您需要处理GridView.HiddenEditor

请参阅this devex文档。