[整理]GRIDVIEW使用初步

收集整理 
1 分页
a把AllowPaging设置为true,并设置每页的分页条数
b  
protected void GridView1_PageIndexChanging( object sender, GridViewPageEventArgs e)
{GridView1.PageIndex = e.NewPageIndex;
Bind();}
2 排序
a设置allowsorting=true,然后设置OnSorting="GridView1_Sorting"
b
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{ViewState["sortexpression"] = e.SortExpression;
if (ViewState["sortdirection"] == null)
{ViewState["sortdirection"] = "asc";}
else
{if (ViewState["sortdirection"].ToString() == "asc")
{
ViewState["sortdirection"] = "desc";}
else
{
ViewState["sortdirection"] = "asc";}
}
Bind();}
3编辑
a在aspx页面中设置OnRowEditing="GridView1_RowEditing"
b
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int empid;
string fname, lname;
empid = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
fname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
lname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

SqlConnection cnn = new SqlConnection(@"data source=localhost;initial catalog=northwind;user id=sa;password=000");
cnn.Open();
SqlCommand cmd = new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid", cnn);
cmd.Parameters.Add(new SqlParameter("@fname",fname));
cmd.Parameters.Add(new SqlParameter("@lname", lname));
cmd.Parameters.Add(new SqlParameter("@empid", empid));
cmd.ExecuteNonQuery();
cnn.Close();

GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bind();
}

4 BIND

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from employees", @"data source=localhost;initial catalog=northwind;user id=sa;password=000");
da.Fill(ds,"employees");
DataView dv = ds.Tables[0].DefaultView;

if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}

GridView1.DataSource=dv;
GridView1.DataBind();




    

转载于:https://www.cnblogs.com/wangyingtao/archive/2008/02/06/1065386.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值