首页 新闻 会员 周边

请教GridView分页再排序的问题!!!

0
悬赏园豆:5 [已关闭问题] 关闭于 2012-03-01 14:49

此GridView具有删除、编辑、排序,再整个分页后,每一页的排序都不对了,是按总共有多少数据排序的,怎么只给当前页排序呢?

<asp:GridView ID="g1" runat="server" AutoGenerateColumns="False" 
_disibledevent="g1_RowDataBound" _disibledevent="g1_RowDeleting"
_disibledevent="g1_Sorting" _disibledevent="g1_PageIndexChanging"
_disibledevent="g1_RowCancelingEdit" _disibledevent="g1_RowEditing"
_disibledevent="g1_RowUpdating">
<Columns>
<asp:BoundField DataField="id" HeaderText="学号" SortExpression="id" />
<asp:BoundField DataField="studentName" HeaderText="姓名"
SortExpression="studentName" />
<asp:BoundField DataField="studentSex" HeaderText="性别"
SortExpression="studentSex" />
<asp:TemplateField HeaderText="年龄" SortExpression="studentAge">
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Bind("studentAge") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labAge" runat="server" Text='<%# Bind("studentAge") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
</asp:GridView>

public SqlConnection conn = new SqlConnection("server=.;database=db_workHard;user=sa;pwd=123;");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitData();
}
}

private void InitData()
{
try
{
conn.Open();
}
catch(Exception ex)
{}
string sql = "select * from tb_Student";
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
sda.Fill(dt);
g1.DataSource = dt;
g1.DataKeyNames = new string[] { "id"};
g1.AllowSorting = true;
if (ViewState["sortExpression"] == null)
ViewState["sortExpression"] = "id";
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = "asc";
dt.DefaultView.Sort = ViewState["sortExpression"] + "" + ViewState["sortDirection"];
g1.AllowPaging = true;
g1.PageSize = 3;
g1.DataBind();
conn.Close();
}

protected void g1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
conn.Open();
}
catch(Exception ex)
{}
string sql = "delete from tb_Student where id=@id";
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddWithValue("@id", g1.DataKeys[e.RowIndex].Value);
comm.ExecuteNonQuery();
InitData();
conn.Close();
}

protected void g1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
Button b = (Button)e.Row.Cells[4].Controls[0];
b.Attributes["onclick"] = "x=confirm('确定要删除" + e.Row.Cells[1].Text + "吗?');if(!x) return;";
}
catch(Exception ex)
{}
}
}

protected void g1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["sortExpression"].ToString() == e.SortExpression)
{
if (ViewState["sortDirection"].ToString() == "asc")
ViewState["sortDirection"] = "desc";
else
ViewState["sortDirection"] = "asc";
}
else
{
ViewState["sortExpression"] = e.SortExpression;
ViewState["sortDirection"] = "asc";
}
InitData();
}

protected void g1_RowEditing(object sender, GridViewEditEventArgs e)
{
g1.EditIndex = e.NewEditIndex;
InitData();
}

protected void g1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
g1.EditIndex = -1;
InitData();
}

protected void g1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
conn.Open();
string stuName = ((TextBox)g1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string stuAge = ((TextBox)g1.Rows[e.RowIndex].Cells[3].Controls[1]).Text;
string sql = "update tb_Student set studentName=@stuName,studentAge=@stuAge where id=@id";
SqlCommand comm = new SqlCommand(sql, conn);
comm.Parameters.AddWithValue("@stuName", stuName);
comm.Parameters.AddWithValue("@stuAge", stuAge);
comm.Parameters.AddWithValue("@id", g1.DataKeys[e.RowIndex].Value);
comm.ExecuteNonQuery();
g1.EditIndex = -1;
InitData();
conn.Close();
}

protected void g1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
g1.PageIndex = e.NewPageIndex;
InitData();
}
luckydd的主页 luckydd | 初学一级 | 园豆:77
提问于:2012-02-23 17:33
< >
分享
所有回答(1)
0

这说明你排序的时候没有跟数据库连接操作。因为你查询出来的数据只有这一页数据,你针对的是对这一页的排序,如果要对所有的数据排序,首先要把所有的查询出来,在排序。或者在数据库中针对所有的数据排序后,在返回你要的多少行数据。

悟行 | 园豆:12559 (专家六级) | 2012-02-23 20:34

我整的这个排序现在就是对所有数据进行的排序,我先整的排序后整的分页,现在是想对当前页的数据排序,不要对所有数据排序。

支持(0) 反对(0) luckydd | 园豆:77 (初学一级) | 2012-02-24 08:43

@blog_doudou: 

对当前页的数据排序,就不要跟数据库联系 了。直接让你绑定GridView的那个集合,用Linq排序,再绑定就可以了。
支持(0) 反对(0) 悟行 | 园豆:12559 (专家六级) | 2012-02-24 09:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册