1

i am newbie to dev express.

i want to change the row background color in dev express grid after the data is loaded in C# winforms.

i am populating the data grid from the below code

    string sReportSql = "SELECT * FROM Employee";
    private void Form_Load(object sender, EventArgs e)
    {

        dataGridView.DataSource = GeDataFromDb();


    }

    private DataTable GeDataFromDb()
    {

        DataTable dtGenericReport = new DataTable();
        using (SqlConnection con = new SqlConnection(connString))
        {
            if (sReportSql != null)
            {
                using (SqlCommand cmd = new SqlCommand(sReportSql, con))
                {
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    dtGenericReport.Load(reader);
                }
            }

        }
        return dtGenericReport;
    }

i tried using the Row Style event but it does not seems to be working

    private void gridview1_RowStyle(object sender, RowStyleEventArgs e)
    {
        GridView View = sender as GridView;
        if (e.RowHandle >= 0) {
            string status = View.GetRowCellDisplayText(e.RowHandle, View.Columns["status"]);
            if (status == "Completed") {
                e.Appearance.BackColor = Color.IndianRed;   
            }
        }
    }
Brijesh
  • 25
  • 3
  • 9

2 Answers2

0

The best way that I know is to use CustomDrawCell event. Something like the following code.

private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
    GridView View = sender as GridView;
    if (e.RowHandle >= 0) {
        string status = View.GetRowCellDisplayText(e.RowHandle, View.Columns["status"]);
        if (status == "Completed") {
            e.Appearance.BackColor = Color.IndianRed;   
        }
    }
}
muludag
  • 134
  • 1
  • 8
  • do you know how do we change the back color of only one particular cell ? – Brijesh Dec 01 '18 at 06:36
  • you can compare particular cell with e.RowHandle and e.Column.ColumnHandle. If your condition is true,just the cell you want to change cell color will change. – muludag Dec 02 '18 at 07:41
  • thank you very much M.U, got the sample code from the below link https://documentation.devexpress.com/WindowsForms/114616/Controls-and-Libraries/Data-Grid/Getting-Started/Walkthroughs/Appearance-and-Conditional-Formatting/Tutorial-Custom-Drawing – Brijesh Dec 02 '18 at 15:04
0

It seems to me that there is no problem in your code

I think the name of event gridview1_RowStyle should be gridview_RowStyle

you may have made a mistake by selecting another gridview

you can check it in Run designer

enter image description here

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
  • it was correct the even is getting called but its not setting the color – Brijesh Dec 01 '18 at 06:03
  • I tested your code and it works fine, for more information take a look at [GridView_RowStyle](https://documentation.devexpress.com/WindowsForms/114615/Controls-and-Libraries/Data-Grid/Getting-Started/Walkthroughs/Appearance-and-Conditional-Formatting/Tutorial-Custom-Styles-for-Rows-and-Cells) – Anas Alweish Dec 01 '18 at 06:10