GridView动态生成模板列

 

转: http://www.cnblogs.com/jeffamy/archive/2006/05/06/392397.html

效果:查看GridView时对某些要修改的数据,直接在网格里修改,点击按钮保存全部修改。
思路:将列(可以修改的))转化成textbox模板列,保存时循环每一条记录保存全部修改。

代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class DynamicItemTemplate : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            
//
绑定GridView1的数据
            SqlConnection myConn = new SqlConnection("user id=sa;password=123;Database =test;data source=z;");
            SqlDataAdapter myAdapter = 
new SqlDataAdapter("select * from tab_user", myConn);
            DataTable dt = 
new DataTable();
            myAdapter.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        SqlConnection myConn = 
new SqlConnection("user id=sa;password=123;Database =test;data source=z;");
        myConn.Open();
        
for (int i = 0; i < GridView1.Rows.Count; i++)
        
{
            
//循环每一行保存全部修改
            TextBox tb = (TextBox)GridView1.Rows[i].FindControl("textbox1");
            SqlCommand comm = 
new SqlCommand();
            comm.Connection = myConn;
            comm.CommandText = "update tab_user set user_pwd = '" + tb.Text.Trim() + "' where user_id = " + GridView1.Rows[i].Cells[0].Text;
            comm.ExecuteNonQuery();
        }
        myConn.Close();
    }


    
/**//*
     *
下面这两个事件里为动态生成textbox列之必须?
     *
两个事件里的代码一样 
     */


    
//当创建行用于呈现(作为 TableRow)时引发
    //
如果没有用此事件而仅用RowDataBound,在页面postback时将找不到控件(因为DataBind()写在!page.ispostback里?)
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        ItemTemplateGenerate("textbox1", 2, e);
    }
    
//当行与数据绑定时引发
    //
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        ItemTemplateGenerate("textbox1", 2, e);
    }

    
/**//// <summary>
    
/// 根据控件id,要生成控件的列动态生成textbox模板列
    
/// </summary>
    
/// <param name="id">模板列内控件的id</param>
    
/// <param name="cellIndex">列的序号索引值</param>
    
/// <param name="e">GridViewRowEventArgs</param>
    protected void ItemTemplateGenerate(string id, int cellIndex, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType != DataControlRowType.Header)
        
{
            TextBox tb = 
new TextBox();
            tb.ID = id;
            tb.BorderWidth = 0;
            tb.Text = e.Row.Cells[cellIndex].Text;
            e.Row.Cells[cellIndex].Controls.Add(tb);
        }   
    }

}

  副作用:1.GridView不能分页;
                        2.
数据多时效率会比较低;
                        3.……
不详……

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值