要修改 DataGridView 的背景色,你可以使用属性 'DefaultCellStyle.BackColor' 或 'RowsDefaultCellStyle.BackColor' 来设置整个 DataGridView 或单独的行的背景色。下面是一个示例代码,演示如何修改 DataGridView 的背景色:

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

class Program
{
    static void Main(string[] args)
    {
        // 创建一个窗口
        Form form = new Form();

        // 创建一个 DataGridView 控件
        DataGridView dataGridView = new DataGridView();
        dataGridView.Dock = DockStyle.Fill;

        // 创建列
        dataGridView.Columns.Add("Name", "Name");
        dataGridView.Columns.Add("Age", "Age");
        dataGridView.Columns.Add("City", "City");

        // 添加行并赋值
        dataGridView.Rows.Add("John", 25, "New York");
        dataGridView.Rows.Add("Alice", 30, "London");
        dataGridView.Rows.Add("Bob", 35, "Paris");

        // 设置整个 DataGridView 的背景色
        dataGridView.DefaultCellStyle.BackColor = Color.LightBlue;

        // 设置单独的行的背景色
        dataGridView.Rows[1].DefaultCellStyle.BackColor = Color.Yellow;

        // 将 DataGridView 添加到窗口中
        form.Controls.Add(dataGridView);

        // 显示窗口
        Application.Run(form);
    }
}

在示例代码中,我们首先创建了一个包含三列的 DataGridView,并添加了几行数据。然后,我们使用 'DefaultCellStyle.BackColor' 设置整个 DataGridView 的背景色为 'Color.LightBlue',使用 'RowsDefaultCellStyle.BackColor' 设置第二行的背景色为 'Color.Yellow'。

通过这种方式,你可以修改 DataGridView 的背景色。

希望这可以帮助你实现修改 DataGridView 的背景色。

C# DataGridView 背景色修改指南 - 设置整行或单个单元格颜色

原文地址: https://www.cveoy.top/t/topic/TfK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录