Change the color of the datagridview row based on the column value.

Source: Internet
Author: User

The following is an example of changing the color of a row based on the value of the datagridview column.

It is not difficult to implement this, and attributes will not always be in what we think. This attribute is hidden in the defaultcellstyle attribute of the row.

Code

Foreach (Datagridviewrow row In Mydatagridview. Rows)
{
String Rowtype = Row. cells [ 0 ]. Value. tostring ();
If (Rowtype =   " Type " )
{
Row. defaultcellstyle. backcolor = Color. Red;
Row. defaultcellstyle. forecolor = Color. White; } Else   If (Rowtype =   " Type B " ) { Row. defaultcellstyle. backcolor = Color. Yellow; Row. defaultcellstyle. forecolor = Color. Black; } }

 

Add the above sectionCodeIn the updatedatagridviewrowcolors () method, you can call the datagridview dview method when binding data or re-Binding data.

 

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.