我有一个GridView,其中Id是GridPa1,在updatePanel中,我已经以编程方式定义了大多数事件,但是在创建用于排序的事件处理程序时我没有问题,你能不能看看我的代码并提出实现排序的方法使用下面数据表的事件?
// Method to BinData to GridView1
private void BindData()
{
string strQuery = "SELECT * FROM [tbl_SignIns] ";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
?????????????????????????????????
}
答案 0 :(得分:0)
我有同样的问题,我这样解决。首先,我在gridview标题上添加了向上和向下箭头。
<asp:TemplateField FooterText="Toplam" HeaderText="---">
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Text="---"></asp:Label><br />
<br />
<div id="myelement">
<asp:ImageButton ID="btnDateUp" runat="server" ImageUrl="~/Admin/skins/Skin_1/images/grid_uparrow.png"
OnClick="btnDateUp_Click" />
<asp:ImageButton ID="btnDateDown" runat="server" ImageUrl="~/Admin/skins/Skin_1/images/grid_downarrow.png"
OnClick="btnDateDown_Click" Style="width: 16px" />
</div>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("OrderDate", "{0:D}") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="210px" />
</asp:TemplateField>
之后,我准备了这样的SQL查询;
protected void btnDateUp_Click(object sender, ImageClickEventArgs e)
{
sortingQuery = " ORDER BY OrderDate DESC ";
if (radioListReport.SelectedValue == "1")
SortGrid();
else
SortQuarterGrid();
updatePanelGrid.Update();
}
protected void btnDateDown_Click(object sender, ImageClickEventArgs e)
{
sortingQuery = " ORDER BY OrderDate ASC ";
if (radioListReport.SelectedValue == "1")
SortGrid();
else
SortQuarterGrid();
updatePanelGrid.Update();
}
我希望它会帮助你。
答案 1 :(得分:0)
您必须创建一个用于排序方向的两个视图状态,另一个用于排序表达式,以便在排序时进行检查。
首先,Sortwise从gridview中检索为“Ascending”和“Descending”,当你想在数据表中存档数据时,你必须使用“ASC”和“DESC”来传递gridview排序方向,然后使用它来恢复它排序数据时的值。
第二,翻转函数将通过排序方向并将其翻转,当你第一次点击columon时它会被用来排序“ASC”,例如第二次你点击它,它必须是“DESC”,这就是为什么我翻转它。
第三,Viewstates用于隐藏上一类的值。
第四,创建一个viewstate来保存PageIndexChanging事件的排序数据表,当然你必须检查viewstate是否为null然后你必须获取数据并绑定它,否则你将必须绑定gridview与已排序的数据表
protected void GridView_Users_Sorting(object sender, GridViewSortEventArgs e)
{
if (String.IsNullOrEmpty(ViewState["sortExpression"].ToString()) && String.IsNullOrEmpty(ViewState["sortDirection"].ToString()))
{
//Sort and bind data as ASCENDING for the first time
DefaultSortBind(e);
}
else
{
if (ViewState["sortExpression"].ToString() == e.SortExpression.ToString())
{
string sortDirection = string.Empty;
if (ViewState["sortDirection"].ToString() == e.SortDirection.ToString())
{
sortDirection = flipSortDirection(GetSortDirection(e.SortDirection.ToString()));
}
else
{
sortDirection = GetSortDirection(e.SortDirection.ToString());
}
DataTable dt = new UserInfoTableTableAdapter().GetData();
dt.DefaultView.Sort = e.SortExpression + " " + sortDirection;
GridView_Users.DataSource = dt.DefaultView;
GridView_Users.DataBind();
ViewState["sortedDt"] = dt.DefaultView.ToTable();
ViewState["sortDirection"] = sortDirection;
}
else
{
//Sort and bind data as ASCENDING
DefaultSortBind(e);
}
}
}
private void DefaultSortBind(GridViewSortEventArgs e)
{
ViewState["sortExpression"] = e.SortExpression;
ViewState["sortDirection"] = e.SortDirection;
//Fetch data again, because if we try to get data from gridview it will give null
DataTable dt = new UserInfoTableTableAdapter().GetData();
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortDirection.ToString());
GridView_Users.DataSource = dt.DefaultView;
GridView_Users.DataBind();
ViewState["sortedDt"] = dt.DefaultView.ToTable();
}
//Get sort direction
private string GetSortDirection(string sortDirection)
{
if (sortDirection == "Ascending")
{
return "ASC";
}
else
{
return "DESC";
}
}
//Flip sort direction
private string flipSortDirection(string sortDirection)
{
if (sortDirection == "ASC")
{
return "DESC";
}
else
{
return "ASC";
}
}