@(编程)
1. 用法
student类
using System.ComponentModel;
namespace WindowsFormsApplication1
{
public class Student
{
public string Id { get; set; }
[DisplayName("姓名")]
public string Name { get; set; }
[DisplayName("性别")]
public string Gender { get; set; }
[DisplayName("年级")]
public string Grade { get; set; }
[DisplayName("班级")]
public string Grade111 { get; set; }
}
}
应用
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<Student> data = new List<Student>();
Student s1 = new Student() { Id = Guid.NewGuid().ToString(), Gender = "男", Grade = "一年级", Name = "小王" };
data.Add(s1);
List<string> hideList = new List<string>();
hideList.Add("Id");
this.wDataGridView1.MyDataList = data;
this.wDataGridView1.MyHideList = hideList;
this.wDataGridView1.MyType = typeof(Student);
this.wDataGridView1.Repaint();
Student entity = this.wDataGridView1.GetSelect0BindItem() as Student;
MessageBox.Show(entity.Name);
}
}
}
2. 源码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace Wisdombud.CommonTool.UI
{
public partial class WDataGridView : DataGridView
{
public IEnumerable<Object> MyDataList { get; set; }
public Type MyType { get; set; }
public IEnumerable<string> MyHideList { get; set; }
public WDataGridView()
{
InitializeComponent();
this.Init();
}
public WDataGridView(Type MyType, IEnumerable<Object> MyDataList, IEnumerable<string> MyHideList)
{
this.MyType = MyType;
this.MyHideList = MyHideList;
this.MyDataList = MyDataList;
InitializeComponent();
this.Init();
}
private void Init()
{
this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.EditMode = DataGridViewEditMode.EditProgrammatically;
this.RowHeadersVisible = false;
this.Dock = DockStyle.Fill;
this.BackgroundColor = System.Drawing.SystemColors.Control;
}
public WDataGridView(IContainer container)
{
container.Add(this);
InitializeComponent();
this.Init();
}
public Object GetSelect0BindItem()
{
if (this.SelectedRows.Count == 0)
{
return null;
}
return this.SelectedRows[0].DataBoundItem;
}
public void Repaint()
{
if (this.MyHideList == null)
{
this.MyHideList = new List<string>();
}
this.DataSource = this.MyDataList;
if (this.MyDataList != null && this.MyType != null)
{
int i = 0;
foreach (PropertyInfo pi in MyType.GetProperties())
{
string captionName = pi.Name;
object[] objs = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true);
if (this.Columns.Count <= i)
{
return;
}
this.Columns[i].Visible = true;
if (this.MyHideList.Contains(pi.Name))
{
this.Columns[i].Visible = false;
}
else
{
this.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
if (objs.Length > 0)
{
captionName = ((DisplayNameAttribute)objs[0]).DisplayName;
}
this.Columns[i].HeaderText = captionName;
}
i++;
if (i == this.Columns.Count)
{
break;
}
}
}
}
}
}