简体   繁体   中英

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

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM