如何遍历/循环通过DevExpress.GridView.gridcontrol的特定列的每个单元格在c# WinFor
本文关键字:WinFor 单元格 gridcontrol GridView 遍历 何遍历 DevExpress 循环 | 更新日期: 2023-09-27 18:04:25
我想遍历 DevExpress.GridView.gridcontrol的特定(已知)列的每个单元格,根据特定的单元格值进行一些验证,到目前为止,我所尝试的如下—
for (int i = 0; i < gridView1.Columns.Count; i++)
{
this.gridView1.Columns[i].AppearanceCell.BackColor = Color.Red;
}
使用上面的代码我可以循环通过DevExpress.GridView.gridcontrol的每个列,但无法循环通过特定(第I)列的每个单元格。希望你能明白我想说的。请帮助. .提前感谢!!
参考资料中有这样的例子:
验证行条件样式格式
显示数据表格。RowStyle事件
显示数据表格。RowCellStyle事件
和一个简单的循环:// iterate cells and compose a tab delimited string of cell values
for (int i = 0; i < m_gridView.RowCount; i++)
{
int rowHandle = m_gridView.GetRowHandle(i);
...
}
// and
for(int i = 0; i < gridView1.DataRowCount; i++)
{
//Do something here
}
look also at:GridView.GetRowCellValue
方法
最后我得到了答案,它是通过使用RowCellStyle事件完成的,如下所示——
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView View = sender as DevExpress.XtraGrid.Views.Grid.GridView;
if (e.Column.FieldName == "XYZ")
{
string strCellValue = View.GetRowCellDisplayText(e.RowHandle, View.Columns["XYZ"]);
int inStockCol = e.RowHandle;
if (!strCellValue.Equals("NA"))
{
double dblCellValue = Convert.ToDouble(strCellValue);
if (dblCellValue < 0 || dblCellValue > 1000)
{
e.Appearance.BackColor = Color.Red;
e.Appearance.BackColor2 = Color.LightPink;
}
else
{
e.Appearance.BackColor = Color.Green;
e.Appearance.BackColor2 = Color.LightGreen;
}
}
}
}