星空网 > 软件开发 > ASP.net

关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

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

先来看看我原有的代码:

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

最终呈现的效果如下:

关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

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

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

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

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

效果如下:

关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

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




原标题:关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流