views:

263

answers:

3

Hi, I have to add a color to particular rows of DevExpress gridview. There is an event RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e). It works fine but if I sort the data, good indexes are lost. How to solve this problem? How to access the sorted data because I only can access datasource. Thank you. Here is the code that works only with not sorted data:

private void dataGridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
    {
       try
       {

        if ((int)((DataTable)gridControl1.DataSource).Rows[e.RowHandle]["Lating"] > 0)
            {
                e.Appearance.BackColor = Color.Red;

            }
       }
        catch
        {

        }

    }
A: 

you want to set the AlternatingRowsDefaultCellStyle for the Data grid.

ex: this.myDataGrid.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle14;

This was assuming that you were doing alternating row colors. Sorry. Your settings should be retained even on a sort, unless you have made your own sort function.

mondaysgeek
I don't think he asks for that but for custom coloring based on the Lating field.
Konstantinos
+1  A: 

take a look at the example provided in the DevEx documentation:

using DevExpress.XtraGrid.Views.Grid;

 private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
    GridView View = sender as GridView;
    if(e.RowHandle >= 0) {
       string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["Category"]);
       if(category == "Beverages") {
          e.Appearance.BackColor = Color.Salmon;
          e.Appearance.BackColor2 = Color.SeaShell;
       }            
    }
 }

As you can notice they access the grid view data on the column and not the datasource directly.

Hope this helps.

Konstantinos
A: 

You either can take the approach Konstantinos described or use

gridView.GetDataRow(e.RowHandle)["Lating"]

to access the underlying data set.

Sven Künzler