c# dataGridView控件里面的DataSource这个属性问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# dataGridView控件里面的DataSource这个属性问题相关的知识,希望对你有一定的参考价值。
c# dataGridView控件里面的DataSource这个属性 给他绑定一个泛型 为什么泛型里面的元素 所指向的类的属性是internal就绑定不了 改为public就可以
internal 关键字是类型和类型成员的访问修饰符。只有在同一程序集的文件中,内部类型或成员才是可访问的。DataGridView绑定DataSource的过程看起来就是一个简单的赋值语句,其实内部有很多不为人知的隐式调用,而调用者与你的泛型类不在同一个程序集中,所以无法访问到internal的属性。 参考技术A internal访问修饰符,仅内部成员能访问,只有这个泛型类能访问它,别的访问不了。 参考技术B internal等于私有,public是公开,datasource绑定的时候和普通读取类属性 也是一样的。
C# Winform DataGridView 控件的基本使用
▲ 运行截图
两个注意点
- 报错:DataGridView 控件中至少有一列没有单元格模板
解决方法:
一个小误区,你看看设计窗体生成的代码,DataGridView 的列不是 GridViewColumn 而是 DataGridViewTextBoxColumn
你只要添加这个类型的对象就可以了,我也是饶了好久才绕出来
- 用
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# dataGridView控件里面的DataSource这个属性问题的主要内容,如果未能解决你的问题,请参考以下文章
C# Winform下,datagridview或者ListView能够自定义模板吗?