如何在gridview Devexpress中从rowselected设置单元格属性?

时间:2015-07-18 02:27:23

标签: gridview devexpress cell selectionchanged

我是devexpress的新手。当我使用gridview时,我遇到了一个问题:

private void grdItem_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
    {
        ReceiveController _rc = new ReceiveController();
        DataRow r = null;
        int[] selectedRows = grdItem.GetSelectedRows();
        if (selectedRows.Length != 0)
        {
            for (int i = 0; i < selectedRows.Length; i++)
            {
                grdItem.GetDataRow(selectedRows[i]);

               // Set cell properties here
            }                
        }
    }

在事件选择中更改了一行,我需要在其中设置一些单元格属性,可能是:bordercolor,allowedit = false或者禁用..... 但我不知道自己该怎么办?

2 个答案:

答案 0 :(得分:1)

如果你真的需要改变特定单元格的布局,我会使用事件

Private Sub myDataGridView_CustomDrawCell(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs) Handles myDataGridView.CustomDrawCell
Dim dgv As DevExpress.XtraGrid.Views.Grid.GridView = CType(sender, DevExpress.XtraGrid.Views.Grid.GridView)
Dim tmpObj As YOURBOUNDOBJECT = CType(dgv.GetRow(e.RowHandle), YOURBOUNDOBJECT)
If Not tmpObj Is Nothing Then
  If tmpObj.DoFormat Then
    e.Appearance.BackColor = Color.LightYellow
    e.Appearance.ForeColor = Color.Red
  End If
End If
End Sub

要启用或禁用单元格,我将分配一个CellEdit控件(在列的属性中)并在显示editControl的情况下启用/禁用它:

Private Sub myGridView_CustomRowCellEdit(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) _
Handles myGridView.CustomRowCellEdit

Dim dgv As DevExpress.XtraGrid.Views.Grid.GridView = CType(sender, DevExpress.XtraGrid.Views.Grid.GridView)
Dim tmpObj As YOURBOUNDOBJECT = dgv.GetRow(e.RowHandle)

If String.Compare(e.Column.FieldName, "nameOfYourColumnToLock", True) = 0 Then
  If not tmpObj.LockColumn Then
    e.RepositoryItem = Me.repTxtEnabled
  Else
    e.RepositoryItem = Me.repTxtDisabled
  End If
End If
...

repTxtEnabled是一个可编辑的RepositoryItemTextEdit,而repTxtDisabled将是一个锁定的RepositoryItemTextEdit

答案 1 :(得分:0)

您不能为此类特定单元格设置外观属性。 GridControl不维护行/列的内部集合,因此它不像只说Grid.Rows [3] .Cells [2] .BackColor = Color.Blue;一样容易。

相反,您需要处理一个GridView的绘图事件;在这种情况下,似乎RowCellStyle event将是最佳选择。

通常,在RowCellStyle事件处理程序中,您将检查当前单元格是否满足基于规则的特定条件。如果是这样,您可以将格式应用于该单元格。