C# Winform DataGridView 控件的基本使用

Posted 胡文杰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
    }
}

以上是关于C# Winform DataGridView 控件的基本使用的主要内容,如果未能解决你的问题,请参考以下文章

c# winform datagridview界面上的行删了,但datagridview数据源没有同步如何解决

c# winform datagridview怎么能达到如图的效果

winform c# Datagridview 选中行 急!!!

C# winform 如何用datagridview增、删、改数据

C# winform DatagridView 的简单操作

c# winform DataGridView添加一行,添加数据后,保存到数据库