Search code examples
c#winformsdatagridviewdevexpressdevexpress-windows-ui

how to change row color in dev express grid after the data is loaded from the database in c# winforms


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

Solution

  • 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;   
            }
        }
    }