【C#语言】DataGridView修改选中行颜色

本文介绍如何在C#中通过处理DataGridView的RowPrePaint事件,自定义选中行的颜色,以满足项目的视觉需求。通过设置DefaultCellStyle属性,可以改变行的默认样式,实现选中行颜色的更改。

        在项目实践过程中,需要修改选中行的颜色,以满足客户的需求。

        事件

重新绘制行的颜色

RowPrePaint.

        属性

获取或设置行的默认样式,除非重写默认样式,否则将用它们来呈现行中的单元格。

   public override DataGridViewCellStyle DefaultCellStyle { get; set; }

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_DataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //全局变量
        int rowIndex;


        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = Source();
        }

        //数据表资源
        private DataTable Source()
        {
            DataTable mydt = new DataTable();
            mydt.Columns.Add("姓名");
            mydt.Columns.Add("年龄");
            mydt.Columns.Add("分数");
            String[,] str = new String[,] { { "张三", "21", "90" }, { "李四", "22", "93" }, { "王五", "23", "99" } };

            for (int i = 0; i < 3; i++)
            {
                DataRow dr = mydt.NewRow();
                dr[0] = str[i, 0];
                dr[1] = str[i, 1];
                dr[2] = str[i, 2];
                mydt.Rows.Add(dr);
            }
            return mydt;

        }

        //获取单元格信息
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取单元格坐标
            int col = dataGridView1.CurrentCellAddress.X + 1;
            int row = dataGridView1.CurrentCellAddress.Y + 1;

            //获取单元格内容
            String content = dataGridView1.CurrentCell.Value.ToString();

            MessageBox.Show("行:" + row.ToString() + "   列:" + col.ToString() + "      内容:" + content);

        }

        //隐藏年龄
        private void button1_Click(object sender, EventArgs e)
        {
            String bts = button1.Text;
            if (bts == "隐藏年龄")
            {
                dataGridView1.Columns[1].Visible = false;
                bts = "显示年龄";
                button1.Text = bts;
            }
            else
            {
                dataGridView1.Columns[1].Visible = true;
                bts = "隐藏年龄";
                button1.Text = bts;
            }

        }

        //右键点击事件
        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];
                this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
                contextMenuStrip1.Show(Cursor.Position);
                rowIndex = e.RowIndex;

            }

        }

        //删除行事件的内容
        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow)
            {
                this.dataGridView1.Rows.RemoveAt(rowIndex);
            }

        }

        //排序按钮
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Descending);
        }

        //筛选按钮
        private void button3_Click(object sender, EventArgs e)
        {
            DataView dv = new DataView(Source(), "姓名='李四'", "年龄", DataViewRowState.CurrentRows);
            dataGridView1.DataSource = dv;
        }


        //修改选中行颜色
        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            int a = e.RowIndex;
            var row = dataGridView1.Rows[a];
            if (row == dataGridView1.CurrentRow)
            {
                row.DefaultCellStyle.BackColor = Color.Red;
            }
            else
            {
                row.DefaultCellStyle.BackColor = Color.White;
            }


        }

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值