【问题标题】:how to change row color in dev express grid after the data is loaded from the database in c# winforms在c#winforms中从数据库加载数据后如何更改dev express网格中的行颜色
【发布时间】:2019-05-02 13:16:16
【问题描述】:

我是开发快递的新手。

我想在 C# winforms 中加载数据后更改 dev express 网格中的行背景颜色。

我正在从下面的代码填充数据网格

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

我尝试使用 Row Style 事件,但它似乎不起作用

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

【问题讨论】:

    标签: c# winforms datagridview devexpress devexpress-windows-ui


    【解决方案1】:

    在我看来你的代码没有问题

    我认为事件的名称gridview1_RowStyle应该是gridview_RowStyle

    您可能通过选择另一个网格视图而犯了一个错误

    你可以在运行设计器中查看

    【讨论】:

    • 偶数被调用是正确的,但它没有设置颜色
    • 我测试了你的代码,它运行良好,更多信息请查看GridView_RowStyle
    【解决方案2】:

    据我所知,最好的方法是使用 CustomDrawCell 事件。类似于以下代码。

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

    【讨论】:

    • 你知道我们如何只改变一个特定单元格的背景颜色吗?
    • 您可以将特定单元格与 e.RowHandle 和 e.Column.ColumnHandle 进行比较。如果您的条件为真,则您要更改单元格颜色的单元格会发生变化。
    • 非常感谢M.U,从下面的链接documentation.devexpress.com/WindowsForms/114616/…得到示例代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多