C# Winform DataGridView 控件的基本使用

▲ 运行截图


两个注意点

  1. 报错:DataGridView 控件中至少有一列没有单元格模板

解决方法

一个小误区,你看看设计窗体生成的代码,DataGridView 的列不是 GridViewColumn 而是 DataGridViewTextBoxColumn
你只要添加这个类型的对象就可以了,我也是饶了好久才绕出来

  1. Paint事件,重绘了dataGridView外边框颜色。

代码

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CsTest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();

            _students.Add(new Student() { Name = "张三", Age = 10, Gender = "男", Chinese = 50, English = 100, Math = 99 });
            _students.Add(new Student() { Name = "李四", Age = 11, Gender = "女", Chinese = 23, English = 78, Math = 80 });
            _students.Add(new Student() { Name = "王五", Age = 15, Gender = "女", Chinese = 23, English = 78, Math = 80 });

            DataGridViewSetting();
        }

        private List<Student> _students = new List<Student>();

        private void DataGridViewSetting()
        {
            DataGridViewSetting(dgv_Test);
        }

        private void DataGridViewSetting(DataGridView dataGridView)
        {
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToDeleteRows = false;
            dataGridView.ReadOnly = true;
            dataGridView.RowHeadersVisible = false; // 控制行头的显示
            dataGridView.BackgroundColor = SystemColors.ButtonFace;
            dataGridView.ColumnHeadersHeight = 25;
            dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;

            //下面这种会抛异常(没有单元格模板), 不能添加 DataGridViewColumn 对象,而是 DataGridViewTextBoxColumn 对象
            //dataGridView.Columns.Add(new DataGridViewColumn() { Name = "Name", HeaderText = "姓名", DataPropertyName = "Name", CellTemplate = new DataGridViewTextBoxCell() });
            //dataGridView.Columns.Add(new DataGridViewColumn() { Name = "Age", HeaderText = "年龄", DataPropertyName = "Age", CellTemplate = new DataGridViewTextBoxCell() });
            //dataGridView.Columns.Add(new DataGridViewColumn() { Name = "Gender", HeaderText = "性别", DataPropertyName = "Gender", CellTemplate = new DataGridViewTextBoxCell() });
            //dataGridView.Columns.Add(new DataGridViewColumn() { Name = "Chinese", HeaderText = "语文", DataPropertyName = "Chinese", CellTemplate = new DataGridViewTextBoxCell() });
            //dataGridView.Columns.Add(new DataGridViewColumn() { Name = "Math", HeaderText = "数学", DataPropertyName = "Math", CellTemplate = new DataGridViewTextBoxCell() });
            //dataGridView.Columns.Add(new DataGridViewColumn() { Name = "English", HeaderText = "英语", DataPropertyName = "English", CellTemplate = new DataGridViewTextBoxCell() });

            dataGridView.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Name", HeaderText = "姓名", DataPropertyName = "Name" });
            dataGridView.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Age", HeaderText = "年龄", DataPropertyName = "Age" });
            dataGridView.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Gender", HeaderText = "性别", DataPropertyName = "Gender" });
            dataGridView.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Chinese", HeaderText = "语文", DataPropertyName = "Chinese" });
            dataGridView.Columns.Add(new DataGridViewTextBoxColumn() { Name = "Math", HeaderText = "数学", DataPropertyName = "Math" });
            dataGridView.Columns.Add(new DataGridViewTextBoxColumn() { Name = "English", HeaderText = "英语", DataPropertyName = "English" });

            // 下面这种方式也可以,不过要多写一行代码

            //dataGridView.Columns.Add("Name", "姓名");
            //dataGridView.Columns["Name"].DataPropertyName = "Name";
            //dataGridView.Columns.Add("Age", "年龄");
            //dataGridView.Columns["Age"].DataPropertyName = "Age";
            //dataGridView.Columns.Add("Gender", "性别");
            //dataGridView.Columns["Gender"].DataPropertyName = "Gender";
            //dataGridView.Columns.Add("Chinese", "语文");
            //dataGridView.Columns["Chinese"].DataPropertyName = "Chinese";
            //dataGridView.Columns.Add("Math", "数学");
            //dataGridView.Columns["Math"].DataPropertyName = "Math";
            //dataGridView.Columns.Add("English", "英语");
            //dataGridView.Columns["English"].DataPropertyName = "English";

            dataGridView.DataSource = _students;
            dataGridView.Paint += dgv_Test_Paint;  // 重绘制dataGridView外边框颜色
        }

        // 重绘制dataGridView外边框颜色
        private void dgv_Test_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Red, new Rectangle(0, 0, this.dgv_Test.Width - 1, this.dgv_Test.Height - 1));
        }
    }

    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public int Chinese { get; set; }
        public int Math { get; set; }
        public int English { get; set; }
    }
}
posted @   double64  阅读(994)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示