GridView自定义编辑、删除、全选删除代码

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;

public partial class
_Default : System.Web.UI.Page
{
    
protected void Page_Load(object
sender, EventArgs e)
     {
        
if (!
IsPostBack)
         {
            
this.GridView1.DataSource =
Membership.GetAllUsers();
            
this
.GridView1.DataBind();
            
this.GridView2.DataSource =
Membership.GetAllUsers();
            
this
.GridView2.DataBind();
         }
     }
    
protected void GridView1_RowCommand(object
sender, GridViewCommandEventArgs e)
     {
        
if (e.CommandName == "add"
)
         {
            
int index =
Convert.ToInt32(e.CommandArgument);
             ListItem item
= new
ListItem();
             item.Text
= Server.HtmlEncode(GridView1.DataKeys[index].Values[0
].ToString());
            
if (!
ListBox1.Items.Contains(item))
             ListBox1.Items.Add(item);
             Label1.Text
=e.CommandArgument.ToString()+GridView1.DataKeys[index].Values[0
].ToString();
            
            
         }
     }
    
protected void GridView1_RowCreated(object
sender, GridViewRowEventArgs e)
     {
        
if (e.Row.RowType ==
DataControlRowType.DataRow)
         {
             LinkButton addButton
= (LinkButton)e.Row.Cells[0].Controls[0
];
             addButton.CommandArgument
=
e.Row.RowIndex.ToString();
            
         }


     }

    
protected void GridView1_RowDataBound(object
sender, GridViewRowEventArgs e)
     {
        
if (e.Row.RowType ==
DataControlRowType.DataRow)
         {
           
string str = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "UserName"
));
            
//满足特定要求的数据显示不同的颜色

            Response.Write(str);
            
if (str=="duoduo"
)
             {
                 e.Row.BackColor
=
System.Drawing.Color.Brown;
             }  
             e.Row.Cells[
1].Text = "<i>" + e.Row.Cells[1].Text + "</i>"
;           
            
//鼠标经过或者离开时候显示不同的颜色

             e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='red',this.style.fontWeight='';" );
            
//当鼠标离开的时候 将背景颜色还原的以前的颜色

             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';" );
           
         }
     }
    
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
     {
         
int index =
Convert.ToInt32(e.RowIndex);
         GridViewRow row
=
GridView1.Rows[index];
         
string duser= row.Cells[2
].Text;
         Membership.DeleteUser(duser);
        
this.GridView1.DataSource =
Membership.GetAllUsers();
        
this
.GridView1.DataBind();
        
//
     }
    
protected void GridView1_RowDeleted(object
sender, GridViewDeletedEventArgs e)
     {
       
     }
    
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
     {
         GridView1.EditIndex
=
e.NewEditIndex;
         GridView1.DataSource
=
Membership.GetAllUsers();
         GridView1.DataBind();
     }
    
protected void GridView1_RowUpdating(object
sender, GridViewUpdateEventArgs e)
     {
        
string username = GridView1.DataKeys[e.RowIndex].Values[0
].ToString();
        
string email = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2"
)).Text;
        
//string name = GridView1.Rows[e.RowIndex].FindControl("TextBox1").ToString();//onlyread

         MembershipUser u= Membership.GetUser(username);
         u.Email
=
email;
        
//u.UserName = name;

         Membership.UpdateUser(u);
         GridView1.EditIndex
= -1
;
        
this.GridView1.DataSource =
Membership.GetAllUsers();
        
this
.GridView1.DataBind();
        
     }
    
protected void GridView1_RowCancelingEdit(object
sender, GridViewCancelEditEventArgs e)
     {
         GridView1.EditIndex
= -1
;
         GridView1.DataSource
=
Membership.GetAllUsers();
         GridView1.DataBind();
     }
    
protected void GridView2_RowEditing(object
sender, GridViewEditEventArgs e)
     {
         GridView2.EditIndex
=
e.NewEditIndex;
         GridView2.DataSource
=
Membership.GetAllUsers();
         GridView2.DataBind();
     }
    
protected void GridView2_RowCancelingEdit(object
sender, GridViewCancelEditEventArgs e)
     {
         GridView2.EditIndex
= -1
;
         GridView2.DataSource
=
Membership.GetAllUsers();
         GridView2.DataBind();
     }
    
protected void GridView2_RowUpdating(object
sender, GridViewUpdateEventArgs e)
     {

     }
    
protected void CheckBox2_CheckedChanged(object
sender, EventArgs e)
     {
        
foreach (GridViewRow gvr in
GridView1.Rows)
         {
             ((CheckBox)gvr.Cells[
5].FindControl("CheckBox1")).Checked =
((CheckBox)sender).Checked;
         }
     }
    
protected void Button1_Click(object
sender, EventArgs e)
     {
        
foreach (GridViewRow gvr in
GridView1.Rows)
         {
        
if (((CheckBox)gvr.Cells[5].FindControl("CheckBox1"
)).Checked)
             {
           
//string duser = ((Label)gvr.Cells[2].FindControl("Label1")).Text;

            string duser = ((Label)gvr.Cells[2].Controls[1 ]).Text;
             Response .Write (duser);
            
//
Membership.DeleteUser(duser);
            
//
this.GridView1.DataSource = Membership.GetAllUsers();
            
//this.GridView1.DataBind();

             }
         }
     }
}

你可能感兴趣的:(GridView)