@(编程)

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;
}
}
}
}
}
}

Wisdombud.CommonTool及其应用的更多相关文章

  1. tomcat RMI 停不掉

    项目采用jfinal框架,做了一个RMI的服务,对其它程序提供服务.实现上,写了一个RmiPlugin package com.wisdombud.cloudtalk.plugin; import j ...

  2. JAVA spring hibernate 多数据源配置记录

    数据源配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  3. java spring 邮件发送

    开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...

  4. iOS 根据银行卡号判断银行名称

    如何根据银行卡号判断银行名称? + (NSString *)getBankName:(NSString*) cardId{ //"发卡行.卡种名称", NSArray* bankN ...

  5. UNITY3D在线更新之道-CSlight 使用总结

    转自:http://blog.csdn.net/leonwei/article/details/39233775 最近做U3D的热更新,研究了各种方式无果后,最容易最先想到的方式就是利用c#的反射机制 ...

  6. Hibernate入门(2)- 不用配置用注解

    在上一个例子里面,我用的配置文件的方式,这次改成注解. pom.xml 增加了hibernate-commons-annotations和hibernate-annotations <proje ...

  7. OpenXML操作word

    OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错.现在开始采用OpenXML.OpenXML(OOXML)是微软在Office 2007中提出的一 ...

  8. c#通过操作mongodb gridfs实现文件的数据库存储

    @(编程) 源码 using MongoDB.Driver; using MongoDB.Driver.GridFS; using System.IO; namespace Wisdombud.Mon ...

  9. C#下载http文件

    @(编程) using System; using System.IO; using System.Net; namespace Wisdombud.Util { public class HttpH ...

随机推荐

  1. xxx cannot be resolved to a type 错误解决方法

    (1)jdk不匹配(或不存在)     项目指定的jdk为“jdk1.6.0_18”,而当前eclipse使用的是“jdk1.6.0_22”.需要在BuildPath | Libraries,中做简单 ...

  2. HDU 3951 (博弈) Coin Game

    先考虑两种简单的情况: 如果先手能一次把硬币拿完,即 k >= n ,那么先手胜 如果每次只能拿一个硬币, 即 k = 1 ,那么如果有奇数个硬币先手胜,如果有偶数个硬币后手胜. 剩下的情况就是 ...

  3. Spring cron 表达式

    前言: 最近做的项目有用到定时器,每周只在特定时间运行一次,考虑到Spring Task的简单易用性,就果断选择了,我是配置在配置文件里面,没有用注解@Scheduled,推荐配置,注解虽方便,但更改 ...

  4. mysql 表日常变化前几

    mysql 表日常变化前几use performance_schema create table test.snap1 as SELECT OBJECT_SCHEMA, OBJECT_NAME, CO ...

  5. Notepad 列编辑、正则查找、替换

    目标: 将源数据转成初始化sql语句.源数据: 104110040018,1,中国银行,中国银行天津琼州道支行,NULL,1100,天津市,12,天津市 104110040059,1,中国银行,中国银 ...

  6. log4j配置文件详细解释

    web.xml中配置启动log4j的配置 <!-- webAppRootKey进行配置,这里主要是让log能将日志写到对应项目根目录下 --> <!-- 定义以后,在Web Cont ...

  7. PHP的cURL库:抓取网页,POST数据及其他,HTTP认证 抓取数据

    From : http://developer.51cto.com/art/200904/121739.htm 下面是一个小例程: ﹤?php// 初始化一个 cURL 对象$curl = curl_ ...

  8. GUID转换成16位字符串或19位数据(确保唯一)

    // <summary> /// 根据GUID获取16位的唯一字符串 /// </summary> /// <param name=\"guid\"& ...

  9. Js对象转String的函数 和 JSON转String

    js对象转string的函数 function obj2str(o){ var r = []; if(typeof o =="string") return "" ...

  10. Informatica9.6.1在Linux Red Hat 5.8上安装遇到的有关问题整理_2

    2. 产品安装过程提示Ping Domain Error 1)错误日志: Pinging domain... 8:19:22 AM ********************************** ...