using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace TestXtraGridAutoBind
{
public partial class Form1 : Form
{
public User[] userArr;
public IList<UserObj> userList;
public Form1()
{
InitializeComponent();
userArr = new User[1];
User ss = new User();
ss.id = 1;
ss.name = "kenter";
ss.pwd = "123456";
ss.status = true;
userArr[0] = ss;
userList = new List<UserObj>();
UserObj ss2 = new UserObj();
ss2.ChannelId = 2;
FieldInfo[] pi = ss.GetType().GetFields();
//foreach (FieldInfo p in pi)
//{
// PropertyInfo v = ss2.GetType().GetProperty(p.Name);
// v.SetValue(ss2, p.GetValue(ss), null);
//}
userList.Add(ss2);
PropertyInfo[] ss2Fi = ss2.GetType().GetProperties();
DevExpress.XtraGrid.Columns.GridColumn[] gridColumnArr =
new DevExpress.XtraGrid.Columns.GridColumn[ss2Fi.Length];
foreach (PropertyInfo p in ss2Fi)
{
DevExpress.XtraGrid.Columns.GridColumn gridColumn =
new DevExpress.XtraGrid.Columns.GridColumn();
object[] tArr = p.GetCustomAttributes(true);
foreach (object to in tArr)
{
if (to is GridFieldAttribute)
{
GridFieldAttribute dd = (GridFieldAttribute)to;
gridColumn.Caption = dd.ShowName;
gridColumn.Name = p.Name;
gridColumn.Visible = !dd.IsHide;
gridColumn.VisibleIndex = dd.SortID;
gridColumnArr[dd.SortID] = gridColumn;
}
}
}
this.gridView1.Columns.Clear();
this.gridView1.Columns.AddRange(gridColumnArr);
this.gridControl1.DataSource = userList;
}
}
public struct User
{
public byte id;
public string name;
public string pwd;
public bool status;
}
public class UserObj
{
private byte channelId;
[GridFieldAttribute(ShowName = "通道", Description = "简介", SortID = 0)]
public byte ChannelId
{
get { return channelId; }
set { channelId = value; }
}
private byte id;
[GridFieldAttribute(ShowName = "序号", Description = "简介", SortID = 1)]
public byte Id
{
get { return id; }
set { id = value; }
}
private string name;
[GridFieldAttribute(ShowName = "名称", Description = "简介", SortID = 2)]
public string Name
{
get { return name; }
set { name = value; }
}
private string pwd;
[GridFieldAttribute(ShowName = "密码", Description = "简介", SortID = 3)]
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
private bool status;
[GridFieldAttribute(ShowName = "状态", Description = "简介", SortID = 4)]
public bool Status
{
get { return status; }
set { status = value; }
}
}
public class GridFieldAttribute : Attribute
{
public string ShowName { get; set; }
public string Description { get; set; }
public bool IsHide { get; set; }
public int SortID { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace TestXtraGridAutoBind
{
public partial class Form1 : Form
{
public User[] userArr;
public IList<UserObj> userList;
public Form1()
{
InitializeComponent();
userArr = new User[1];
User ss = new User();
ss.id = 1;
ss.name = "kenter";
ss.pwd = "123456";
ss.status = true;
userArr[0] = ss;
userList = new List<UserObj>();
UserObj ss2 = new UserObj();
ss2.ChannelId = 2;
FieldInfo[] pi = ss.GetType().GetFields();
//foreach (FieldInfo p in pi)
//{
// PropertyInfo v = ss2.GetType().GetProperty(p.Name);
// v.SetValue(ss2, p.GetValue(ss), null);
//}
userList.Add(ss2);
PropertyInfo[] ss2Fi = ss2.GetType().GetProperties();
DevExpress.XtraGrid.Columns.GridColumn[] gridColumnArr =
new DevExpress.XtraGrid.Columns.GridColumn[ss2Fi.Length];
foreach (PropertyInfo p in ss2Fi)
{
DevExpress.XtraGrid.Columns.GridColumn gridColumn =
new DevExpress.XtraGrid.Columns.GridColumn();
object[] tArr = p.GetCustomAttributes(true);
foreach (object to in tArr)
{
if (to is GridFieldAttribute)
{
GridFieldAttribute dd = (GridFieldAttribute)to;
gridColumn.Caption = dd.ShowName;
gridColumn.Name = p.Name;
gridColumn.Visible = !dd.IsHide;
gridColumn.VisibleIndex = dd.SortID;
gridColumnArr[dd.SortID] = gridColumn;
}
}
}
this.gridView1.Columns.Clear();
this.gridView1.Columns.AddRange(gridColumnArr);
this.gridControl1.DataSource = userList;
}
}
public struct User
{
public byte id;
public string name;
public string pwd;
public bool status;
}
public class UserObj
{
private byte channelId;
[GridFieldAttribute(ShowName = "通道", Description = "简介", SortID = 0)]
public byte ChannelId
{
get { return channelId; }
set { channelId = value; }
}
private byte id;
[GridFieldAttribute(ShowName = "序号", Description = "简介", SortID = 1)]
public byte Id
{
get { return id; }
set { id = value; }
}
private string name;
[GridFieldAttribute(ShowName = "名称", Description = "简介", SortID = 2)]
public string Name
{
get { return name; }
set { name = value; }
}
private string pwd;
[GridFieldAttribute(ShowName = "密码", Description = "简介", SortID = 3)]
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
private bool status;
[GridFieldAttribute(ShowName = "状态", Description = "简介", SortID = 4)]
public bool Status
{
get { return status; }
set { status = value; }
}
}
public class GridFieldAttribute : Attribute
{
public string ShowName { get; set; }
public string Description { get; set; }
public bool IsHide { get; set; }
public int SortID { get; set; }
}
}