0

我正在使用 devexpress winforms 网格。

我将数据集的数据绑定到 devexpress 网格。

 dataGrid.MainView.GridControl.DataSource = ds;
 dataGrid.MainView = gridView;
 gridView.BestFitColumns();

网格显示会是这样的

 FirstName  LastName    
 Sharp      Eye

我调用 RowStyle 事件来根据条件显示网格中行的背景颜色。

例如:

private void gridViewExcel_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
    GridView View = sender as GridView;
    string firstName = View.GetRowCellDisplayText(e.RowHandle, View.Columns["FirstName"]);

      if (firstName="Sharp")
      {
        e.Appearance.BackColor = Color.Salmon;
        e.Appearance.BackColor2 = Color.White;
      } 
     else
     {
        //I want to append another column in the end to the dataset that is bound to the grid.
        //With an error message...(see below)
     }
}

输出

 FirstName LastName     Message

 Sharp        Eye      First name doesn't match
4

3 回答 3

1

您不能有条件地在特定行上显示额外的列。解决您的问题的一种方法是在将其绑定到网格之前将“消息”列添加到数据源。如果一行有错误,然后更改此列中的文本。

于 2013-10-09T18:07:06.323 回答
1

您可以在运行时实例化 DevExpress.XtraGrid.Columns.GridColumn()。

前任 :

DevExpress.XtraGrid.Columns.GridColumn colMessage= newDevExpress.XtraGrid.Columns.GridColumn();

colMessage.Caption = "Message";
colMessage.FieldName = "<bound datafield>";
colMessage.Name = "colMessage";
colMessage.OptionsColumn.AllowEdit = false;
colMessage.OptionsColumn.FixedWidth = true;
colMessage.OptionsColumn.ReadOnly = true;
colMessage.Visible = true;
colMessage.VisibleIndex = 0;
colMessage.Width = 80;
View.Columns.AddRange(newDevExpress.XtraGrid.Columns.GridColumn[] {
            this.colMessage});

也可以声明一个未绑定的列并用 gridView 事件填充它

于 2013-10-09T18:10:21.833 回答
1

我不熟悉 devexpress,但我认为在将数据绑定到网格之前分析数据会更好。何时调用 gridViewExcel_RowStyle?如果频繁调用它,您希望避免繁重的逻辑。

如果您真的想要这里的逻辑,您应该能够从绑定的源(或从字段)获取数据集并根据需要添加列。

GridView View = sender as GridView;
    string firstName = View.GetRowCellDisplayText(e.RowHandle, View.Columns["FirstName"]);

      if (firstName=="Sharp")
      {
        e.Appearance.BackColor = Color.Salmon;
        e.Appearance.BackColor2 = Color.White;
      } 
     else
 {   

     DataSet ds = view.DataSource as DataSet;
     if(ds != null)
     {
         //add the column to the table you want if it doesn't exist
     }
 }
于 2013-10-09T18:11:09.447 回答