About the reasons and solutions for C # Winform DataGridView setting DefaultCellStyle invalid

Source: Internet
Author: User

Last week in the development of the WinForm project, I encountered a seemingly simple, but has not solved the problem, that is: Set WinForm DataGridView Control of the row DefaultCellStyle, but there is no change, I have turned to Bo Q:/http q.cnblogs.com/q/72294/, but the answer is no one can solve the problem, it may be that the problem is not easy to reproduce, I myself have repeatedly tried to reproduce the problem in other projects, but has been correct, did not appear my current project problems, it is almost a crash!

Let's take a look at my original code:

        private void Form1_Load (object sender, EventArgs e) {Datagridview1.setheader<zwj.tems.entiti Es.                        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,//here demonstrates directly ignoring the condition t = = new {T.assetsingleno, t.assetsing                                             LeInfo.BaseInfo.Name, T.assetsingleinfo.baseinfo.category.categoryname, T.assetsingleinfo.price, T.assetsinglein Fo.                            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)}; }

The resulting results are as follows:

As you can see from the table above, there is no effect on line 2nd (index 1, which is actually line 2nd). Of course, if you copy the code and the table to other projects, there may not be such a problem, which is a very annoying thing. In order to solve this simple problem, make clear the reason, today, a work, I began to test and continue to find answers on the internet, finally Kung Fu, finally found in the community of Microsoft people also mentioned such problems, and resolved, the address is: https://social.microsoft.com /forums/zh-cn/d928e42d-9e10-4b1a-b2ee-2694894f47af/datagridview?forum=visualcshartzhchs, it says:

Re-put all the bound data in the display again, here a little delay, resulting in the color is not actually set to the correct display on the row. In databindingcompleted time, make sure that all the data has been bound to completion, and that this will ensure that the correct line is set.

The cause of the problem was found, the original is bound, the data has a delay, you must confirm that the data binding is complete before you can set the style, for this reason, I fixed the code, the original set style code into the Databindingcomplete event, as follows:

        private void Form1_Load (object sender, EventArgs e) {Datagridview1.setheader<zwj.tems.entiti Es.                        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,//here demonstrates directly ignoring the condition t = = new {T.assetsingleno, t.assetsing                                             LeInfo.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)}; }

The effect is as follows:

The problem finally solved, although is a small problem, but if not understand the principle and find the source of the problem, small problems will become a big problem, so from this issue to warn me and everyone: do not despise any problem, to have the determination of the inquisitive, every problem to find the root cause, not only to know how to do, Also understand why you should do this, so that you can grow.

About the reasons and solutions for C # Winform DataGridView setting DefaultCellStyle invalid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.