c# datagridview绑定

  1   public   class  GridEntity
  2      {
  3        public GridEntity() 
  4        {
  5        }

  6
  7        private string _GridName;
  8        private string _DataPropertyName;
  9        private string _HeaderText;
 10        private string _ColumnType;
 11        private int _Width;
 12        private bool _IsFrozen;
 13        private int _SortNo;
 14        #region 
 15        /**//// <summary>
 16        /// 列表名
 17        /// </summary>

 18        public string GridName
 19        {
 20            set
 21            {
 22                _GridName = value;
 23            }

 24            get
 25            {
 26                
 27                if (_GridName == null)
 28                {
 29                    return null;
 30                }

 31                else
 32                {
 33                    return _GridName.Trim();
 34                }

 35            }

 36        }

 37        #endregion

 38        
 39        #region 
 40        /**//// <summary>
 41        /// 绑定字段名
 42        /// </summary>

 43        public string DataPropertyName
 44        {
 45            set
 46            {
 47                _DataPropertyName = value;
 48            }

 49            get
 50            {
 51                
 52                if (_DataPropertyName == null)
 53                {
 54                    return null;
 55                }

 56                else
 57                {
 58                    return _DataPropertyName.Trim();
 59                }

 60            }

 61        }

 62        #endregion

 63        
 64        #region 
 65        /**//// <summary>
 66        /// 列名
 67        /// </summary>

 68        public string HeaderText
 69        {
 70            set
 71            {
 72                _HeaderText = value;
 73            }

 74            get
 75            {
 76                
 77                if (_HeaderText == null)
 78                {
 79                    return null;
 80                }

 81                else
 82                {
 83                    return _HeaderText.Trim();
 84                }

 85            }

 86        }

 87        #endregion

 88        
 89        #region 
 90        /**//// <summary>
 91        /// 列类型
 92        /// </summary>

 93        public string ColumnType
 94        {
 95            set
 96            {
 97                _ColumnType = value;
 98            }

 99            get
100            {
101                
102                if (_ColumnType == null)
103                {
104                    return null;
105                }

106                else
107                {
108                    return _ColumnType.Trim();
109                }

110            }

111        }

112        #endregion

113        
114        #region 
115        /**//// <summary>
116        /// 宽度
117        /// </summary>

118        public int Width
119        {
120            set
121            {
122                _Width = value;
123            }

124            get
125            {
126                return _Width;
127            }

128        }

129        #endregion

130        
131        #region 
132        /**//// <summary>
133        /// 列是否冻结
134        /// </summary>

135        public bool IsFrozen
136        {
137            set
138            {
139                _IsFrozen = value;
140            }

141            get
142            {
143                return _IsFrozen;
144                
145            }

146        }

147        #endregion

148        
149        #region 
150        /**//// <summary>
151        /// 排序
152        /// </summary>

153        public int SortNo
154        {
155            set
156            {
157                _SortNo = value;
158            }

159            get
160            {
161                return _SortNo;
162            }

163        }

164        #endregion

165
166
167        
168    }

2 Grid操作类
public   class  GridBind
    
{
        
public GridBind() 
        
{
            
        }


        
/**//// <summary>
        
/// 给DataGridView添加列
        
/// </summary>
        
/// <param name="eDataGridView"></param>
        
/// <param name="eList"></param>

        public static void AddColumn(DataGridView eDataGridView, List<GridEntity> eList)
        
{
            
foreach (GridEntity mGridEntity in eList) 
            
{
                eDataGridView.Columns.Add(getColumn(mGridEntity));
            }

        }


        
/**//// <summary>
        
/// 取得要添加的列
        
/// </summary>
        
/// <param name="eGridEntity"></param>
        
/// <returns></returns>

        private static DataGridViewColumn getColumn(GridEntity eGridEntity)
        
{
            
switch (eGridEntity.ColumnType) 
            
{
                
case "Bool":
                    DataGridViewColumn mColBool 
= new DataGridViewCheckBoxColumn();
                    mColBool.Name 
= eGridEntity.DataPropertyName;
                    mColBool.DataPropertyName 
= eGridEntity.DataPropertyName;
                    mColBool.HeaderText 
= eGridEntity.HeaderText;
                    mColBool.Width 
= eGridEntity.Width;
                    mColBool.Frozen 
= eGridEntity.IsFrozen;
                    
return mColBool;
                
default:
                    DataGridViewColumn mColTextBox 
= new DataGridViewTextBoxColumn();
                    mColTextBox.Name 
= eGridEntity.DataPropertyName;
                    mColTextBox.DataPropertyName 
= eGridEntity.DataPropertyName;
                    mColTextBox.HeaderText 
= eGridEntity.HeaderText;
                    mColTextBox.Width 
= eGridEntity.Width;
                    mColTextBox.Frozen 
= eGridEntity.IsFrozen;
                    
return mColTextBox;
                   
            }

        }

    }

3 调用方法

/**/ /// <summary>
        
/// 绑定列表
        
/// </summary>
        
/// <param name="eDataGridView">DataGridView名</param>
        
/// <param name="eGridName">在Sys_GridList数据表中对应的列表名</param>

         protected   void  BaseBindGrid(DataGridView eDataGridView,  string  eGridName)
        
{
            eDataGridView.Columns.Clear();
            DAL.BaseForm.DALGrid mDALGrid 
= new DY.DyErp.DAL.BaseForm.DALGrid();
            List
<Common.GridEntity> mGridEntityList = new List<DY.Common.GridEntity>();
            
foreach (DataRow mDataRow in mDALGrid.getColumnList(eGridName).Rows)
            
{
                Common.GridEntity mGridEntity 
= new DY.Common.GridEntity();
                mGridEntity.GridName 
= mDataRow["GridName"].ToString();
                mGridEntity.DataPropertyName 
= mDataRow["DataPropertyName"].ToString();
                mGridEntity.HeaderText 
= mDataRow["HeaderText"].ToString();
                mGridEntity.Width 
= Convert.ToInt32(mDataRow["Width"]);
                mGridEntity.ColumnType 
= mDataRow["ColumnType"].ToString();
                mGridEntity.IsFrozen 
= Convert.ToBoolean(mDataRow["IsFrozen"]);
                mGridEntity.SortNo 
= Convert.ToInt32(mDataRow["SortNo"]);
                mGridEntityList.Add(mGridEntity);
            }

            DY.Common.GridBind.AddColumn(eDataGridView, mGridEntityList);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值