原地址
1.点击标题排序 2.分页
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
...
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
3编辑
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
...
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
4更新功能
//
更新(- -口水吧功能),设置了OnRowEditing="GridView1_RowEditing"。
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
{
int id;
string xiaozhu;
// 下面得到主建
id = int .Parse(GridView1.Rows[e.RowIndex].Cells[ 0 ].Text);
// 下面得到更新的内容
fxiaozhu; = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 1 ].Controls[ 0 ]).Text;
SqlConnection cnn = new SqlConnection( " 保密 " );
try
{
cnn.Open();
// 这里写更新
SqlCommand cmd = new SqlCommand( " 保密 " )
cmd.ExecuteNonQuery();
} catch ()
{
}
finally
{
cnn.Close();
}
GridView1.EditIndex = - 1 ;
BindGrid();
}
protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e)
{
int id;
string xiaozhu;
// 下面得到主建
id = int .Parse(GridView1.Rows[e.RowIndex].Cells[ 0 ].Text);
// 下面得到更新的内容
fxiaozhu; = ((TextBox)GridView1.Rows[e.RowIndex].Cells[ 1 ].Controls[ 0 ]).Text;
SqlConnection cnn = new SqlConnection( " 保密 " );
try
{
cnn.Open();
// 这里写更新
SqlCommand cmd = new SqlCommand( " 保密 " )
cmd.ExecuteNonQuery();
} catch ()
{
}
finally
{
cnn.Close();
}
GridView1.EditIndex = - 1 ;
BindGrid();
}
5删除
protected
void
GridView1_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
...
{
int index = e.RowIndex;
DataKey key = GridView1.DataKeys[index];
int id = Convert.ToInt32(key.Value.ToString());
//省略..因为偶懒的写了~~呵呵
GridView1.EditIndex = -1;
BindGrid();
}
//
为了实现每列都可以自动点击排序,可以设置allowsorting=true,然后设置OnSorting="GridView1_Sorting",或者直接点事件~~别告诉我你不知道在那里~~那来的砖头
protected
void
GridView1_Sorting(
object
sender, GridViewSortEventArgs e)
...
{
ViewState["sortexpression"] = e.SortExpression;
//设置viewsate来保存每次排序时的顺序
if (ViewState["sortdirection"] == null)
...{
ViewState["sortdirection"] = "asc";
}
else
...{
if (ViewState["sortdirection"].ToString() == "asc")
...{
ViewState["sortdirection"] = "desc";
}
else
...{
ViewState["sortdirection"] = "asc";
}
}
BindGrid();
}
本文介绍了ASP.NET中GridView控件的使用方法,包括分页、排序、编辑、更新及删除等核心功能的实现细节。
1188

被折叠的 条评论
为什么被折叠?



