zoukankan      html  css  js  c++  java
  • 动态绑定表格

    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 { getset; }
            
    public string Description { getset; }
            
    public bool IsHide { getset; }
            
    public int SortID { getset; }
        }
    }
  • 相关阅读:
    new 做了什么
    create-react-app+react-app-rewired引入antd实践
    实战build-react(二)-------引入Ant Design(增加)
    package.json文件
    读书多些会怎样
    关于学历
    QDialog弹出一个窗口,改变窗口大小
    #include <thread>
    #include <memory>
    #include <map>
  • 原文地址:https://www.cnblogs.com/kenter/p/2053911.html
Copyright © 2011-2022 走看看