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

最终呈现的效果如下:

从上面的表格中可以看出,第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) };
        }

效果如下:

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

时间: 2024-08-13 23:20:57

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

解决winform datagridview的ClearSelection无效问题

因为把方法放在了界面的构造方法里,此时datagridview还没绘制出来,所以ClearSelection方法无效,放在control或form的load方法里就没问题了 参考:https://www.cnblogs.com/qfl-blog/p/9956843.html https://blog.csdn.net/kasama1953/article/details/52164565 原文地址:https://www.cnblogs.com/haizine/p/10566742.html

UIView设置阴影无效的原因之一

本想在底部的按钮设置个阴影, 代码如下: self.layer.shadowColor = [UIColor blackColor].CGColor; self.layer.shadowOffset = CGSizeMake(-5, 5); self.layer.shadowOpacity = 0.8; self.layer.shadowRadius = 10; 写完后,死活不出效果,那个纠结啊,明明代码没问题啊 最后发现,按钮没有设置背景色,把背景色设置后,就好!

jquery mobile页面跳转后,JS无效的原因及解决方案

最近在做个项目,用到jquery mobile,很陌生对他,问题一个个的来,那就要一个个解决,找了一天这个问题,放可明白:首先明白jqm里面页面跳转默认都是通过ajax请求的,必须重新刷新页面js方可有效,也就是js没有起作用,并不是js本身的问题,下面说说解决方法:在使用jQuery Mobile进行Web开发中,当页面跳转时(pageA => pageB),在pageB中引用的JS并未成功运行.因为,JQM并为将整个页面加载到当前的dom中,仅将data-role="page"

[Winform]线程间操作无效,从不是创建控件的线程访问它的几个解决方案,async和await?

目录 概述 取消跨线程检查 使用委托异步调用 sync和await 总结 概述 最近在qq群里有一朋友,问起在winform中怎么通过开启线程的方式去处理耗时的操作,比如,查看某个目录下所有的文件,或者符合要求的文件.下班回来,也研究了一下.发现多线程这块有点薄弱,也算是补一补吧. 在winform开发,经常会遇到需要在控件上加载大量数据(也就是常说的耗时操作),这会导致程序出现假死状态,这个时候我们就会想到线程. 在智能客户端应用程序中,这样的线程创建并管理用户界面 (UI),因而称为 UI

C# winform DataGridView 操作大全

C# DataGridView控件动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时.可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行.假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法: 方法一: int index=this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[

C# winform dataGridView

1 private void btnConn_Click(object sender, EventArgs e) 2 { 3 //定义连接字符串 4 string constr = "server=.;database=DBTest;uid=sa;pwd=sa;"; 5 SqlConnection con = new SqlConnection(constr); 6 try 7 { 8 con.Open(); 9 SqlCommand cmd = new SqlCommand(&quo

C# WinForm DataGridView界面设计小技巧

在窗口中表格是非常常见的数据显示形式,所以界面的展示效果非常重要,通过多次的使用之后发现C# WinForm DataGridView控件默认的显示样式非常之反人类,不过好在可视化操作只需几个简单的属性修改就能得到很好的效果. 下面请看生成的默认属性的DataGridView显示: 非常之反人类的表格. 开始修改下面几个通过名字就能够读懂的属性(当然是通过VS属性窗口修改,也可以在初始化代码中手动修改DataGridView的属性): AllowUserToAddRows=False; //不同

WinForm DataGridView分页功能

WinForm 里面的DataGridView不像WebForm里面的GridView那样有自带的分页功能,需要自己写代码来实现分页,效果如下图: 分页控件  .CS: 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.T

c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行

c# winform DataGridView单击选中一整行,只能单选,不能选择多行,只能选择一行 设置DataGridView的属性SelectionMode为FullRowSelect?这样就使DataGridView不是选择一个字段,而是选择一整行了? 设置DataGridView的属性MultiSelect为false?这样就使DataGridView不能够选择多行,只能选择一行了 想得到某列的值是要判断DataGridView是否有选中的行 if (dataGridView1.Sele