如何从 gridview Devexpress 中的 rowselected 设置单元格属性?
How to set cell properties from rowselected in gridview Devexpress?
我是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 或 disable.....
但是我不知道怎么办?
您不能像这样为特定单元格设置外观属性。 GridControl 不维护 rows/columns 的内部集合,因此它不像说 Grid.Rows[3].Cells[2].BackColor = Color.Blue; 那样简单。
相反,您需要处理 GridView 的绘图事件之一;在这种情况下,RowCellStyle event 似乎是最佳选择。
通常,在 RowCellStyle 事件处理程序中,您将根据规则检查当前单元格是否满足特定条件。如果是这样,您可以将格式应用于该单元格。
如果您确实需要更改特定单元格的布局,我会使用事件
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 控件(在列的属性中)和 enable/disable 在显示 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
我是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 或 disable..... 但是我不知道怎么办?
您不能像这样为特定单元格设置外观属性。 GridControl 不维护 rows/columns 的内部集合,因此它不像说 Grid.Rows[3].Cells[2].BackColor = Color.Blue; 那样简单。
相反,您需要处理 GridView 的绘图事件之一;在这种情况下,RowCellStyle event 似乎是最佳选择。
通常,在 RowCellStyle 事件处理程序中,您将根据规则检查当前单元格是否满足特定条件。如果是这样,您可以将格式应用于该单元格。
如果您确实需要更改特定单元格的布局,我会使用事件
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 控件(在列的属性中)和 enable/disable 在显示 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