DataGridView改变行的颜色

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wuma0q1an/article/details/50832806

DataGridView里面有个属性RowsDefaultCellStyle -- DataGridViewRow{ Index} 




其中,外观里面可以设置各种属性,当然也可以代码实现,下面是示例

        //染色事件
        void dgvApplyData_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            DataGridViewRow row = this.dgvApplyData.Rows[e.RowIndex];
            DataRow datarow = (row.DataBoundItem as DataRowView).Row;

            string clacode = datarow["cla_code"].ToString();
            DataRow[] selectRows = colordatatable.Select("cla_code ='" + clacode + "'");
            if (selectRows.Count() <= 0)
                return;
            string color = selectRows[0]["cla_color"].ToString();
            row.Cells["cla_name"].Style.ForeColor = System.Drawing.ColorTranslator.FromHtml(color);
            row.Cells["app_flag_name"].Style.ForeColor = Color.Red;  //修改颜色
            row.Cells["app_flag_name"].Style.SelectionForeColor = Color.Red;  //选中行颜色
            if (ConvertHelper.IntParse(datarow["app_flag"]) > 3)
            {
                row.Cells["app_flag_name"].Style.ForeColor = Color.Green;
                row.Cells["app_flag_name"].Style.SelectionForeColor = Color.Green;
            }
            if (ConvertHelper.IntParse(datarow["ac_perbook_flag"]) == 20)
            {
                row.DefaultCellStyle.ForeColor = Color.Red;
                row.DefaultCellStyle.SelectionForeColor = Color.Red;
            }
            

        }


猜你喜欢

转载自blog.csdn.net/wuma0q1an/article/details/50832806