C # DataGridView Reading marks for specified line text bold

Source: Internet
Author: User

When you use the DataGridView control to place information such as notifications, you encounter an issue that marks "read", "unread." In the results queried by the SQL statement, read and unread are placed in a dedicated field (DataGridView column) to mark the reading of this entry. The goal of this article is to differentiate the current user's read and unread entries on display.

1. Preparatory work

Build a C # form application that puts a dock property set to full DataGridView

2. Program code

In the load function, the simulation generates a data source, and the code that handles the DataGridView display state is placed in the DataSourceChanged event, which refreshes the display state of DataGridView whenever DataSource is re-assigned. Determines whether the row data is read by the value of the Isread column in the code

using system;using system.collections.generic;using system.componentmodel;using  system.data;using system.drawing;using system.linq;using system.text;using  System.text.regularexpressions;using system.threading.tasks;using system.windows.forms;namespace  test{    public partial class FormMain : Form     {        public formmain ()          {             InitializeComponent ();        }         private void formmain_load (object sender, eventargs e)          {            //Generating a data source           &Nbsp; datatable dttestdata = new datatable ();             dttestdata.columns.add ("Title");             dttestdata.columns.add ("Content");             dttestdata.columns.add ("Isread");             dttestdata.rows.add ("Title 1",  "Body 1",  true);             dttestdata.rows.add ("Title 2",  "Body 2",  false);             dttestdata.rows.add ("Title 3",  "Body 3",  true);             dttestdata.rows.add ("Heading 4",  "Body 4",  false);             dttestdata.rows.add ("Heading 5" ,  "Body 5",  true);             this.dgvtestdata.datasource =  dttestdata;        }        // Changes in data sources in/ <summary>        ///  data sheets          /// </summary>        ///  <param name= "Sender" ></param>        ///  <param name= "E" ></param>        private void  dgvtestdata_datasourcechanged (object sender, eventargs e)          {            for  (int  i = 0; i < dgvtestdata.rows.count; i++)              {                 String isread = dgvtestdata.rows[i]. cells["Isread"]. Value.tostring ();                 if  (Isread.tolower (). Trim ()  ==  "true")                  {                     //TODO  Read Processing                      dgvtestdata.rows[i]. defaultcellstyle.forecolor = color.black;                     dgvtestdata.rows[i]. Defaultcellstyle.backcolor = color.white;       &nbSp;            dgvtestdata.rows[i]. defaultcellstyle.font =                          new font ("The song Body", 9F,  Fontstyle.regular);                 }                else                 {                     // todo  non-read processing                      dgvtestdata.rows[i]. defaultcellstyle.forecolor = color.black;                     dgvtestdata.rows[i]. defaultcellstyle.backcolor = color.white;                     dgvtestdata.rows[i]. defaultcellstyle.font =                          new font ("The song Body", 9F,  Fontstyle.bold | fontstyle.underline);                 }            }         }    }}

If you do not need to work on a row, but only need to make a style change to a cell, you can write it as follows:

Dgvtestdata.rows[i]. cells["XXX"]. Style.forecolor = Color.black;dgvtestdata.rows[i]. cells["XXX"]. Style.backcolor = Color.white;dgvtestdata.rows[i]. cells["XXX"]. Style.font = new Font ("Arial", 9F, Fontstyle.regular);

3. Running the sample

Determines whether the specified row is bold and underlined, based on the value of the Isread column, such as:


END

C # DataGridView Reading marks for specified line text bold

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.