软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > ASP教程 > GridView的增删改查和分页排序

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;

using System.Data.SqlClient;

public partial class GridView_Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
ViewState["sort"] = "asc";

Bind();
}

}
/// <summary>
/// 绑定GridView控件
/// </summary>
private void Bind()
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=chapter5;Integrated Security=True");
conn.Open();
SqlDataAdapter myda = new SqlDataAdapter("select * from student", conn);
DataTable dt = new DataTable();
myda.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
private void Bind(string sname)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=chapter5;Integrated Security=True");
conn.Open();
string sql = string.Format("select * from student order by {0}",sname);
SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
DataTable dt = new DataTable();
myda.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
/// <summary>
/// 实现分页 页面下面改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Bind();
}
/// <summary>
/// 编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
/// <summary>
/// 取消编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}
/// <summary>
/// 更新事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
int age = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=chapter5;Integrated Security=True");
conn.Open();
string sql = string.Format("update student set s_name = '{0}',s_age = {1} where s_id={2}",name,age,id);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
Bind();

// Page.RegisterStartupScript("duihuakuang", "<script>alert('修改成功!')</script>");
}
//删除 liehuo_net 烈.火网防采集
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=chapter5;Integrated Security=True");
conn.Open();
string sql = string.Format("delete student where s_id={0}",id);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
Bind();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState != DataControlRowState.Edit) //判断行类型是否是数据行 行状态是否是处于编辑状态
{

// ((ImageButton)e.Row.Cells[4].FindControl("ImageButton1")).Attributes.Add("onclick", "return confirm('确定删除')");
((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "return confirm('确定删除')");
e.Row.Attributes.Add("onmouseover", "this.style.color='red'");
e.Row.Attributes.Add("onmouseout", "this.style.color='#000000'");
}
}

 

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["sort"].ToString() == "asc")
{
ViewState["sort"]= "desc";
}
else
{
ViewState["sort"] = "asc";
}
Bind(e.SortExpression);
}
}

    相关阅读
    网友评论