How to set cell properties from rowselected in gridview Devexpress?

I'm a newbie with devexpress. When I work with gridview in it I have a problem :

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
            }                
        }
    }

In event selectionchanged a row, i need set a few cells properties in it, may be : bordercolor, allowedit = false, or disable..... But I don't know how I can do ?

Upvotes: 0

Views: 1083

Answers (2)

ronaldk6
ronaldk6

Reputation: 21

If you really need to change the layout of specific cells I would use the event

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

To enable or disable a cell, I would assign a CellEdit Control (in the properties of the column) and enable/disable this in the event for showing the 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 would be an editable RepositoryItemTextEdit whereas repTxtDisabled would be a locked RepositoryItemTextEdit

Upvotes: 1

Brendon
Brendon

Reputation: 1259

You cannot set appearance properties for a specific cell like that. The GridControl does not maintain an internal collection of rows/columns, so it's not as easy as just saying Grid.Rows[3].Cells[2].BackColor = Color.Blue;.

Instead, you need to handle one of the GridView's drawing events; in this case, it would seem that the RowCellStyle event would be the best choice.

Typically, within the RowCellStyle event handler you will check to see if the current cell meets a specific condition based on a rule. If so, you can then apply formatting to that cell.

Upvotes: 0

Related Questions