C# – How to append columns in the grid dynamically for devexpress datagrid

cdevexpressnetwinforms

I am using devexpress winforms grid.

I bind data from a dataset to the devexpress grid.

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

Grid display would be like this

 FirstName  LastName    
 Sharp      Eye

I call RowStyle event to display background color for rows in the grid based on condition.

For ex:

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

Output

 FirstName LastName     Message

 Sharp        Eye      First name doesn't match

Best Answer

You can't conditionally show an extra column on specific rows. One approach for your problem would be to add a "Message" column to the DataSource before you bind it to the Grid. And then change the text in this column if a row has an error.