GridView自定义编辑、删除

 在HTML页里:

 

< asp:GridView ID = " GridView1 "  runat = " server "  OnPageIndexChanging = " GridView1_PageIndexChanging "
        AutoGenerateColumns
= " False "   AllowPaging = " True "  PageSize = " 5 "  OnRowDeleting = " GridView1_RowDeleting "  DataKeyNames = " uid "  OnRowCancelingEdit = " GridView1_RowCancelingEdit "  OnRowUpdating = " GridView1_RowUpdating "  OnRowEditing = " GridView1_RowEditing "   >
        
< Columns >
                    
< asp:TemplateField HeaderText = " 名称 "  SortExpression = " uname " >
                                
< EditItemTemplate >
                                    
< asp:TextBox ID = " TextBox1 "  runat = " server "  Text = ' <%# Bind("uname") %> ' ></ asp:TextBox >
                                
</ EditItemTemplate >
                                
< ItemTemplate >
                                
<% # DataBinder.Eval(Container.DataItem,  " uname " ) %></ a >
                                
</ ItemTemplate >
                            
</ asp:TemplateField >   
                    
< asp:TemplateField HeaderText = " 年龄 "  SortExpression = " uname " >
                        
< EditItemTemplate >
                            
< asp:TextBox ID = " txtAge "  runat = " server "  Text = ' <%# Bind("age") %> ' ></ asp:TextBox >
                       
                        
</ EditItemTemplate >
                        
< ItemTemplate >
                            
< asp:Label ID = " Label2 "  runat = " server "  Text = ' <%# Bind("age") %> ' ></ asp:Label >
                        
</ ItemTemplate >
                    
</ asp:TemplateField >
                    
< asp:TemplateField HeaderText = " 性别 "  SortExpression = " sex " >
                        
< EditItemTemplate >
                            
< asp:TextBox ID = " txtSex "  runat = " server "  Text = ' <%# Bind("sex") %> ' ></ asp:TextBox >
                       
                        
</ EditItemTemplate >
                        
< ItemTemplate >
                            
< asp:Label ID = " Label3 "  runat = " server "  Text = ' <%# Bind("sex") %> ' ></ asp:Label >
                        
</ ItemTemplate >
                    
</ asp:TemplateField >
                    
< asp:TemplateField HeaderText = " 操作 "  ShowHeader = " False " >
                        
< EditItemTemplate >
                            
< asp:LinkButton ID = " LinkButton1 "  runat = " server "  CausesValidation = " True "  CommandName = " Update "
                                 Text
= " 更新 " ></ asp:LinkButton >
                            
< asp:LinkButton ID = " LinkButton2 "  runat = " server "  CausesValidation = " False "  CommandName = " Cancel "
                                Text
= " 取消 " ></ asp:LinkButton >
                        
</ EditItemTemplate >
                        
< ItemTemplate >
                            
< asp:LinkButton ID = " LinkButton1 "  runat = " server "  CausesValidation = " False "  CommandName = " Edit "
                                Text
= " 编辑 "   ></ asp:LinkButton >
                        
                            
< asp:LinkButton ID = " LinkButton3 "  runat = " server "  CausesValidation = " False "  CommandName = " Delete "
                                Text
= " 删除 "  OnClientClick = " return confirm('确认要删除吗?'); " ></ asp:LinkButton >
                        
</ ItemTemplate >
                        
</ asp:TemplateField >
                
</ Columns >
        
</ asp:GridView >

在后台代码里:

 

public   void  GridViewDataBind()
    
{
        
try
        
{
            
            SqlDataAdapter da 
= new SqlDataAdapter("select * from test", conn);
            DataSet ds 
= new DataSet();
            da.Fill(ds, 
"customers");
            
if (ds.Tables[0].Rows.Count == 0)
            
{
                AddDummyData(ds);
            }

            GridView1.DataSource 
= ds.Tables["customers"];
            GridView1.AllowPaging 
= true;
            GridView1.PageSize 
= 5;
            GridView1.DataBind();

        }

        
catch(Exception ex)
        
{
            ex.ToString();
        }


    }

    
protected   void  GridView1_PageIndexChanging( object  sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        GridViewDataBind();
    }

    
private   void  AddDummyData(DataSet ds)
    
{

        
//如果没有数据显示空行

        DataTable dt 
= ds.Tables[0];

        DataRow newRow 
= dt.NewRow();

        dt.Rows.Add(newRow);

    }


    
protected   void  GridView1_RowDeleting( object  sender, GridViewDeleteEventArgs e)
    
{//删除行
        
//得到ID号
        string rowToDelete = GridView1.DataKeys[e.RowIndex].Values[0].ToString();

        
string str = "DELETE FROM test where uid=" + "'" + rowToDelete + "'" + "";
        SqlDataAdapter da 
= new SqlDataAdapter(str,conn);
        DataSet ds 
= new DataSet();
        da.Fill(ds, 
"customers");
        GridView1.DataSource 
= ds.Tables["customers"];
        GridView1.DataBind();
        GridViewDataBind();

    }

    
protected   void  GridView1_RowCancelingEdit( object  sender, GridViewCancelEditEventArgs e)
    
{//取消编辑状态
        GridView1.EditIndex = -1;
        GridViewDataBind();
    }

    
protected   void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)
    
{
        
string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        
string str = "update test set uname=@uname,age=@age,sex=@sex where uid='"+ID+"'";
        SqlCommand comm 
= new SqlCommand(str, conn);
        SqlParameter parm1 
= new SqlParameter("@uname", SqlDbType.VarChar, 50);
        parm1.Value 
= ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
        SqlParameter parm2 
= new SqlParameter("@age", SqlDbType.Int);
        parm2.Value 
= ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text;
        SqlParameter parm3 
= new SqlParameter("@sex", SqlDbType.VarChar,50);
        parm3.Value 
= ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSex")).Text;
        comm.Parameters.Add(parm1);
        comm.Parameters.Add(parm2);
        comm.Parameters.Add(parm3);
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();

        
this.GridView1.EditIndex = -1;
        GridViewDataBind(); 
    }

    
protected   void  GridView1_RowEditing( object  sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        GridViewDataBind();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值