ID      Subject     Marks
1000    English       65
1000    Physics       70
1000    Chemistry     75
1001    English       78
1001    Physics       71
1001    Chemistry     60
1002    English       85
1002    Physics       80
1002    Chemistry     79

I want to give background colors (in an alternative manner) based on the value of the ID. If the entries are for 1000, then i would like to give 1000 entries a background color, say for example cyan, then for ID 1001 entries, a different color, then again for 1002, give the same background color(or may be a different one).

I use this below code, however, i don't get the background colors correctly. I get all the rows in the grid with background color as cyan.

Any thoughts?

private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
                    ColumnView View = dataGrid.MainView as ColumnView;
                        DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["ID"];
                        for (int i = 0; i <= gridView1.DataRowCount - 1; i++)
                        {
                            if (gridView1.GetRowCellValue(i, col) != gridView1.GetRowCellValue((i + 1), col))
                            {
                                e.Appearance.BackColor = Color.LightCyan;
                            }
                        if (i == gridView1.DataRowCount) break;
                        }

        }
有帮助吗?

解决方案

The RowStyle event is already raised for each row - http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsGridGridView_RowStyletopic

RowStyleEventArgs e (e.RowHandle) can also be negative when dealing with RowGroups http://documentation.devexpress.com/#WindowsForms/CustomDocument695

Each iteration of your loop was using e.RowHandle without checking for negative values - which was changing the back color of the entire column.

I modified your code below to a version that produces an alternating row color pattern on even rows based on the ID column.

     private void dataGrid_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
    {
        if (e.RowHandle >= 0)
        {
            ColumnView View = dataGrid.MainView as ColumnView;
            DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["ID"];
            if (Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, col)) % 2 == 0)
            {
                e.Appearance.BackColor = Color.LightCyan;
            }
            else
            {
                e.Appearance.BackColor = Color.White;
            }
        }
    }

其他提示

Been a while since I worked with DX stuff, but that event looks like something that's raised per row and so what your code is actually doing is repainting multiple times - I would guess this is possibly part of the problem.

Check this link, it shows how to do it: http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridGridView_RowStyletopic

AS you will see in the example they give, you can address the current row in this handler, and you don't need to loop all the rows.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top