For the reason why C # Winform DataGridView sets DefaultCellStyle to be invalid and the solution,

Source: Internet
Author: User

For the reason why C # Winform DataGridView sets DefaultCellStyle to be invalid and the solution,

During Winform development last week, I encountered a seemingly simple but unsolved problem: Setting the DefaultCellStyle row of the winform dview control has not changed, I also asked YU Bo for help: Sorry!

Let's take a look at my original code:

Private void Form1_Load (object sender, EventArgs e) {maid header <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, // ignore the 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); maid = resultList; int entityInListIndex = 1; maid [entityInListIndex]. defaultCellStyle = new maid () {ForeColor = Color. blue, Font = new Font ("Arial", 11F, FontStyle. bold )};}

The final result is as follows:

From the table above, we can see that 2nd rows (index is 1, actually 2nd rows) have no effect. Of course, if you copy the code and tables to other projects, this problem may not occur, which is very annoying. To solve this simple problem, I figured out the reason. When I went to work today, I started testing again and continued to search for answers online. Finally, I did my best, finally, I found someone in the Microsoft community also mentioned this problem and solved it. The address is: https://social.microsoft.com/Forums/zh-CN/d928e42d-9e10-4b1a-b2ee-2694894f47af/datagridview? Forum = visualcshartzhchs, as mentioned here:

Re-display all the bound data. There is a delay, and the color is not set to the correct row. In the DatabindingCompleted time, make sure that all the data has been bound. In this case, make sure that the data is set on the correct row.

The cause of the problem was found. After binding, there was a delay in data. You must confirm that the data binding is complete before you can set the style. For this reason, I fixed the code, put the original Style code in the DataBindingComplete event, as follows:

Private void Form1_Load (object sender, EventArgs e) {maid header <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); maid + = new maid (maid); LoadData ();} private void LoadData () {var resultList = QueryBusiness <ProcureExport>. getList (t => true, // ignore the 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,); maid = resultList;} private void maid databindingcomplete (object sender, subject e) {int entityInListIndex = 1; maid [entityInListIndex]. defaultCellStyle = new maid () {ForeColor = Color. blue, Font = new Font ("Arial", 11F, FontStyle. bold )};}

The effect is as follows:

The problem has finally been solved. Although it is a small problem, if you do not understand the principle and find the root cause of the problem, it will become a major problem. Therefore, tell me and you from this question: do not underestimate any problem. You must have a thorough determination to find the root cause of each problem. You must not only know how to do it, but also understand why it is necessary to grow.

Related Article

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.