winform gridview表行、单元格变色

本文介绍如何在WinForms应用中使用DataGridView控件,并根据特定条件改变行及单元格的颜色。通过RowPrePaint事件,可以实现在不同状态下的颜色变化,例如“已审核”行显示为红色,“已上传”的单元格显示为绿色。
实现对winfrom 下gridview(datagridview)表根据某行某单元格的值来改变当前行和当前行某个单元格的颜色
具体实现如下:
 #region 表行、单元格变色
        private void data_report_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            string status = data_report.Rows[e.RowIndex].Cells["cl_checkstatus"].Value.ToString();
            string uploadstate = data_report.Rows[e.RowIndex].Cells["cl_upload_state"].Value.ToString();
            switch (status)
            {
                case "已审核":
                    data_report.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;  //整行颜色
                    break;
               
            }
            switch (uploadstate)
            {
                case "已上传":
                    data_report.Rows[e.RowIndex].Cells["cl_upload_state"].Style.ForeColor = Color.Green;  //某个单元格颜色
                    break; 
            }
        }
        #endregion
在 C# 的 WinForm 中,`DataGridView` 本身没有直接设置单元格内文本距的属性。不过,可以通过自定义单元格绘制的方式来实现调整文本距的效果。以下是一个示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace DataGridViewLineSpacing { public partial class Form1 : Form { public Form1() { InitializeComponent(); dataGridView1.CellPainting += DataGridView1_CellPainting; } private void DataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground); string cellValue = e.Value?.ToString(); if (!string.IsNullOrEmpty(cellValue)) { // 设置距,这里设置为字体高度的 1.5 倍 float lineSpacing = e.CellStyle.Font.Height * 1.5f; StringFormat format = new StringFormat(); format.Alignment = e.CellStyle.Alignment.ToContentAlignment().ToHorizontalAlignment(); format.LineAlignment = e.CellStyle.Alignment.ToContentAlignment().ToVerticalAlignment(); Rectangle textRect = e.CellBounds; textRect.Inflate(-2, -2); using (Brush foreColorBrush = new SolidBrush(e.CellStyle.ForeColor)) { string[] lines = cellValue.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); float yOffset = textRect.Top; foreach (string line in lines) { e.Graphics.DrawString(line, e.CellStyle.Font, foreColorBrush, new RectangleF(textRect.X, yOffset, textRect.Width, lineSpacing), format); yOffset += lineSpacing; } } } e.Handled = true; } } } public static class ContentAlignmentExtensions { public static HorizontalAlignment ToHorizontalAlignment(this ContentAlignment alignment) { switch (alignment) { case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: return HorizontalAlignment.Left; case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: return HorizontalAlignment.Center; case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: return HorizontalAlignment.Right; default: return HorizontalAlignment.Left; } } public static VerticalAlignment ToVerticalAlignment(this ContentAlignment alignment) { switch (alignment) { case ContentAlignment.TopLeft: case ContentAlignment.TopCenter: case ContentAlignment.TopRight: return VerticalAlignment.Top; case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleRight: return VerticalAlignment.Middle; case ContentAlignment.BottomLeft: case ContentAlignment.BottomCenter: case ContentAlignment.BottomRight: return VerticalAlignment.Bottom; default: return VerticalAlignment.Top; } } } } ``` 在这个示例中,通过处理 `DataGridView` 的 `CellPainting` 事件,自定义了单元格内容的绘制过程。通过调整 `lineSpacing` 变量的值,可以改变文本的距。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值