C# DataGridView 对指定行文字加粗实现阅读标记

在使用DataGridView控件放置通知等信息时,会遇到标记“已读”、“未读”的问题。通过SQL语句查询出的结果中,“已读”、“未读”会被放在一个专门的字段(DataGridView的列)中用来标记这个 条目的阅读情况。本文的目标就是要做到在显示上区分当前用户已读和未读的条目。

1、准备工作

建立一个C#窗体应用程序,里面放置一个Dock属性设置为Full的DataGridView

2、程序代码

在Load函数中,模拟生成了一个数据源,处理DataGridView显示状态的代码放在DataSourceChanged事件中,即每当DataSource重新被赋值时刷新DataGridView的显示状态。代码中按IsRead列的值判断该行数据是否为“已读”

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)
        {
            //生成数据源
            DataTable dtTestData = new DataTable();

            dtTestData.Columns.Add("Title");
            dtTestData.Columns.Add("Content");
            dtTestData.Columns.Add("IsRead");

            dtTestData.Rows.Add("标题1", "正文1", true);
            dtTestData.Rows.Add("标题2", "正文2", false);
            dtTestData.Rows.Add("标题3", "正文3", true);
            dtTestData.Rows.Add("标题4", "正文4", false);
            dtTestData.Rows.Add("标题5", "正文5", true);

            this.dgvTestData.DataSource = dtTestData;
        }

        /// <summary>
        /// 数据表内数据源发生变化
        /// </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 已读处理
                    dgvTestData.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                    dgvTestData.Rows[i].DefaultCellStyle.BackColor = Color.White;
                    dgvTestData.Rows[i].DefaultCellStyle.Font = 
                        new Font("宋体", 9F, FontStyle.Regular);
                }
                else
                {
                    //TODO 未读处理
                    dgvTestData.Rows[i].DefaultCellStyle.ForeColor = Color.Black;
                    dgvTestData.Rows[i].DefaultCellStyle.BackColor = Color.White;
                    dgvTestData.Rows[i].DefaultCellStyle.Font = 
                        new Font("宋体", 9F, FontStyle.Bold | FontStyle.Underline);
                }
            }
        }
    }
}

如果不需要对某行操作,而是只需要对某个单元格做出样式上的改变,可以写成下面这样:

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("宋体", 9F, FontStyle.Regular);

3、运行示例

根据IsRead列的值判断指定行是否加粗加下划线,效果如下图:

205731_PFNf_1425762.png

END

转载于:https://my.oschina.net/Tsybius2014/blog/485335

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值