0
点赞
收藏
分享

微信扫一扫

c# - - - DataGridView 中设置奇偶行不同的颜色(交替行颜色)

要在 DataGridView 中设置奇偶行不同的颜色(交替行颜色),可以通过配置 DefaultCellStyleAlternatingRowsDefaultCellStyle 这两个属性来实现,操作简单且效果好。

关键代码说明:

  • 奇数行颜色:通过 DefaultCellStyle.BackColor 设置,示例中使用白色(Color.White
  • 偶数行颜色:通过 AlternatingRowsDefaultCellStyle.BackColor 设置,示例中使用淡蓝色(Color.AliceBlue
  • 颜色选择建议
    • 选择对比度低的颜色组合(如白色和淡蓝),避免视觉疲劳
    • 确保文字颜色(ForeColor)与背景色有足够对比度,保证可读性
    • 可以用 Color.FromArgb(240, 248, 255) 自定义RGB颜色,实现更柔和的效果

这种设置方式会自动应用到所有行,包括后续动态添加的行,且性能高效,不需要额外的事件处理。

代码

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DataGridViewDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitDataGridView();
            LoadData();
        }

        private void InitDataGridView()
        {
            // 基础设置
            dataGridViewRooms.AutoGenerateColumns = false; // 当 DataGridView 绑定数据源时,禁用根据数据源的字段自动创建列
            dataGridViewRooms.AllowUserToAddRows = false; // 禁止用户在表格末尾手动添加新行
            dataGridViewRooms.RowHeadersVisible = false; // 隐藏行标题
            dataGridViewRooms.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 列自动填充
            dataGridViewRooms.ReadOnly = true; // 只读模式
            dataGridViewRooms.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 整行选择

            // 1. 设置奇数行(默认行)样式
            dataGridViewRooms.DefaultCellStyle.BackColor = Color.White; // 奇数行背景色
            dataGridViewRooms.DefaultCellStyle.ForeColor = Color.Black; // 文字颜色
            dataGridViewRooms.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue; // 选中时背景色

            // 2. 设置偶数行(交替行)样式
            dataGridViewRooms.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue; // 偶数行背景色
            dataGridViewRooms.AlternatingRowsDefaultCellStyle.ForeColor = Color.Black; // 文字颜色
            dataGridViewRooms.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.LightSkyBlue; // 选中时背景色

            // 3. 固定行高并禁止调整
            dataGridViewRooms.RowTemplate.Height = 35;
            dataGridViewRooms.AllowUserToResizeRows = false;

            // 添加列
            dataGridViewRooms.Columns.Add(new DataGridViewTextBoxColumn
            {
                Name = "RoomId",
                HeaderText = "机房ID",
                DataPropertyName = "RoomId"
            });
            dataGridViewRooms.Columns.Add(new DataGridViewTextBoxColumn
            {
                Name = "RoomName",
                HeaderText = "机房名称",
                DataPropertyName = "RoomName"
            });
            dataGridViewRooms.Columns.Add(new DataGridViewTextBoxColumn
            {
                Name = "Status",
                HeaderText = "状态",
                DataPropertyName = "Status"
            });
        }

        private void LoadData()
        {
            // 示例数据
            var data = new[] {
                new { RoomId = "1", RoomName = "一号机房", Status = "正常" },
                new { RoomId = "2", RoomName = "二号机房", Status = "不能报到" },
                new { RoomId = "3", RoomName = "三号机房", Status = "正常" },
                new { RoomId = "4", RoomName = "四号机房", Status = "不能报到" },
                new { RoomId = "5", RoomName = "五号机房", Status = "正常" }
            };
            dataGridViewRooms.DataSource = data;
        }
    }
}

举报

相关推荐

0 条评论