zoukankan      html  css  js  c++  java
  • 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

    上周在开发Winform 项目中,我曾遇到一个看似简单,但一直都没有解决的问题,那就是:设置winform DataGridView控件的行DefaultCellStyle,但却没有任何变化,我也曾求助于博问:http://q.cnblogs.com/q/72294/,但大家给的答案没有一个能解决这个问题,可能是问题重现不太容易,我自己也曾多次在其它项目中尝试重现这个问题,但一直都是正确的,没有出现我当前项目的问题,简直要崩溃啊!

    先来看看我原有的代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.SetHeader<Zwj.TEMS.Entities.AssetDetail>(t => t.AssetSingleNo, t => t.BaseInfo.Name, t => t.BaseInfo.Category.CategoryName,
                    t => t.Price, t => t.ProcureImport.Date, t => t.State.State);
     
        LoadData();
    }
     
    private void LoadData()
    {
        var resultList = QueryBusiness<ProcureExport>.GetList(t =>true,//这里演示就直接忽略条件
                                     t => new
                                     {
                                         t.AssetSingleNo,
                                         t.AssetSingleInfo.BaseInfo.Name,
                                         t.AssetSingleInfo.BaseInfo.Category.CategoryName,
                                         t.AssetSingleInfo.Price,
                                         t.AssetSingleInfo.ProcureImport.Date,
                                         t.AssetSingleInfo.State.State
                                     },t =>t.AssetSingleNo,1,10);
        dataGridView1.DataSource = resultList;
     
        int entityInListIndex = 1;
        dataGridView1.Rows[entityInListIndex].DefaultCellStyle = new DataGridViewCellStyle() { ForeColor = Color.Blue, Font = new Font("Arial", 11F, FontStyle.Bold) };
    }

    最终呈现的效果如下:

    从上面的表格中可以看出,第2行(索引为1,实际为第2行)没有任何效果。当然如果你将这些代码及表格复制到其它项目中,可能不会出现这样的问题,这就是很烦人的事情。为了解决这个简单问题,搞清楚原因,今天一上班,我又开始进行测试与继续在网上找答案,终于功夫不负有心人,终于在微软的社区中发现有人也提到这样的问题,并解决了,地址是:https://social.microsoft.com/Forums/zh-CN/d928e42d-9e10-4b1a-b2ee-2694894f47af/datagridview?forum=visualcshartzhchs,这里面提到:

    重新把所有绑定的数据在显示一遍,这里有一点延时,导致颜色其实没有设置到正确显示的row上。在DatabindingCompleted 时间里面,确保所有的数据 都已经绑定完成,这时候 能够确保 设置在正确的 行上面。

    问题原因找到了,原来是绑定后,数据有延迟,必须确认数据绑定完成后,才能进行样式设置,基于这个原因,我修正了一下代码,将原设置Style的代码放到DataBindingComplete事件中,如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.SetHeader<Zwj.TEMS.Entities.AssetDetail>(t => t.AssetSingleNo, t => t.BaseInfo.Name, t => t.BaseInfo.Category.CategoryName,
                    t => t.Price, t => t.ProcureImport.Date, t => t.State.State);
     
        dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
     
        LoadData();
    }
     
    private void LoadData()
    {
        var resultList = QueryBusiness<ProcureExport>.GetList(t =>true,//这里演示就直接忽略条件
                                     t => new
                                     {
                                         t.AssetSingleNo,
                                         t.AssetSingleInfo.BaseInfo.Name,
                                         t.AssetSingleInfo.BaseInfo.Category.CategoryName,
                                         t.AssetSingleInfo.Price,
                                         t.AssetSingleInfo.ProcureImport.Date,
                                         t.AssetSingleInfo.State.State
                                     },t =>t.AssetSingleNo,1,10);
        dataGridView1.DataSource = resultList;           
    }
     
    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        int entityInListIndex = 1;
        dataGridView1.Rows[entityInListIndex].DefaultCellStyle = new DataGridViewCellStyle() { ForeColor = Color.Blue, Font = new Font("Arial", 11F, FontStyle.Bold) };
    }

    效果如下:

    问题终于解决了,虽然是一个小问题,但若不明白原理及找到问题根源,小问题也会变成大问题,所以从这个问题中告诫我自己及大家:不要轻视任何一个问题,要有刨根问底的决心,每一个问题都要找到根本原因,不仅要知道如何做,还要明白为什么要这样做,这样才会成长。

  • 相关阅读:
    JSTL EL 详解
    什么是JavaBean
    easy.jsp出现According to TLD or attribute directive in tag file, attribute value does not accept any expressions
    搭建sendmail
    系统运维工程师之路
    centos7 搭建安装zabbix3.0服务端实例(一)
    单例模式
    cassandra-压缩策略
    cassandra的坑-windows平台压缩策略
    Quick Sort
  • 原文地址:https://www.cnblogs.com/soundcode/p/12291236.html
Copyright © 2011-2022 走看看