Gridview实现添加更新及鼠标移动事件

using System.Data.SqlClient;


public partial class gridview添加删除更新 : System.Web.UI.Page
{
    //string c;
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            databind();
        }
    }
   private  void databind()
    {
        cn.Open();
        string str= "select u_id,u_name,u_sex,u_age from users";
        SqlDataAdapter sda = new SqlDataAdapter(str, cn);
        DataSet ds = new DataSet();
        sda.Fill(ds, "users");
        GridView1.DataSource = ds.Tables ["users"].DefaultView ;
        GridView1.DataBind();
        cn.Close();
    }
 
   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       this.GridView1.PageIndex = e.NewPageIndex;
       databind();

   }
   protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       string id = this.GridView1.DataKeys[e.RowIndex][0].ToString();
       string name = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text .ToString ();
       string sex = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text .ToString();
       string age = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text .ToString();
       update(id,name, sex, age);
           this .GridView1 .EditIndex =-1;
       databind ();

 

   }
   public void update(string u_id, string u_name, string u_sex, string u_age)
   {
       cn.Open();
       SqlCommand cmd = new SqlCommand("update users set u_name='" + u_name + "',u_sex='" + u_sex + "',u_age='" + u_age + "' where u_id='"+u_id +"'",cn );
       cmd.ExecuteNonQuery();
       cn.Close();
   }
   protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
   {
       this.GridView1.EditIndex = -1;
       databind();
   }
   protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
   {
      
   }
   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
   {
       this.GridView1.EditIndex = e.NewEditIndex;
       databind();
   }
   protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00ffee';");
           e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
       }
   }
}

 

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"
            AutoGenerateColumns="False" PagerSettings-PageButtonCount="5" BackColor="White"
            BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
            AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging"
             PageSize="5" onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleted="GridView1_RowDeleted" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating"
            DataKeyNames="u_id,u_name,u_sex,u_age" onrowcreated="GridView1_RowCreated"
           >

<PagerSettings PageButtonCount="5"></PagerSettings>

            <FooterStyle BackColor="#99CCCC" ForeColor="#003399"/>
            <Columns>
                <asp:BoundField DataField="u_id" HeaderText="用户编号" ReadOnly ="true" />
                <asp:BoundField DataField="u_name" HeaderText="用户姓名" />
                <asp:BoundField DataField="u_sex" HeaderText="用户性别" />
                <asp:BoundField DataField="u_age" HeaderText="用户年龄" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
            <RowStyle BackColor="White" ForeColor="#003399" />
          
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
       
        </asp:GridView>
    </div>
    </form>
</body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值