DataGridView绑定

今早又一杰作,虽然不完美,但还凑合用,不作过多注释,给初学者动脑筋的机会,学会看懂别人的代码和思路,特别像我这些土代码,更可以磨练你的耐心 -_-#

1 Grid 实体类
  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        
 38        
 39        
 63        
 64        
 88        
 89        
113        
114        
130        
131        
148        
149        
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);
        }
posted @ 2007-06-20 11:30  sharelai  阅读(43719)  评论(5编辑  收藏  举报