zoukankan      html  css  js  c++  java
  • 用List绑定GridView的简单辅助类

    使用前提,知道gridview通用的扩展方法,知道反射的知识。

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Collections;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Reflection;
    
    /// <summary>
    ///MyGridView 的摘要说明
    /// </summary>
    public class MyGridView
    {
        GridView gv;
        IList list;
        Type t;
        object obj;
        public MyGridView( GridView gv,IList list,object obj)
        {
            this.gv = gv;
            this.list = list;
            this.obj = obj;
            if (list.Count > 0)
            {
                t = list[0].GetType();
            }
        }
    
    
        #region BindEvents
    
        public void BindEvents()
        {
            gv.RowEditing += new GridViewEditEventHandler(gv_RowEditing);
            gv.RowCancelingEdit += new GridViewCancelEditEventHandler(gv_RowCancelingEdit);
            gv.RowDeleting += new GridViewDeleteEventHandler(gv_RowDeleting);
            gv.RowUpdating += new GridViewUpdateEventHandler(gv_RowUpdating);
        }
    
        void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = 0;
            PropertyInfo[] proInfo = t.GetProperties();
            foreach (TableCell cell in gv.Rows[e.RowIndex].Cells)
            {
                TextBox txtBox = cell.Controls[0] as TextBox;
                if (txtBox != null)
                {
                    proInfo[i].SetValue(list[e.RowIndex], txtBox.Text, null);
                }
                
                i++;
            }
            obj.GetType().GetMethod("Save").Invoke(obj, null);
            
            gv.EditIndex = -1;
        }
    
        void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            list.RemoveAt(e.RowIndex);
        }
    
        void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv.EditIndex = -1;
        }
    
        void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv.EditIndex = e.NewEditIndex;
        }
        #endregion
    
        public void BindData()
        {
            gv.DataSource = list;
            gv.DataBind();
        }
    }
    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Reporting Services系列三:几个细节
    Tips&Tricks系列一:更改VS2005设置
    Reporting Services系列二:引用.NET DLL
    数据库基础系列之六:因空间不足导致IMP失败
    .NET基础示例系列之十四:C#导出建表语句及数据
    Reporting Services系列四:折叠报表
    数据库基础系列之七:IMP数据到指定的表空间
    .NET基础示例系列之十六:制做进程监视器
    .NET基础示例系列之十九:Dundas For ASP.NET
    .NET基础示例系列之十五:操作Excel
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1497961.html
Copyright © 2011-2022 走看看