0

我在 updatePanel 中有一个 ID 为 GridView1 的 GridView,我已经以编程方式定义了大多数事件,但是在创建用于排序的事件处理程序时我没有问题,您能否请看我的代码并建议使用方法来实现排序事件下面的数据表?

// 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)
     {

?????????????????????????????????

     }
4

2 回答 2

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();
    }

我希望它会帮助你。

于 2012-06-01T07:53:32.177 回答
0

您必须创建两个视图状态,一个用于排序方向,另一个用于排序表达式,以便在排序时进行检查。

首先,从 gridview 检索排序方向为“升序”和“降序”,当您想要在数据表中归档数据时,您必须使用传递 gridview 排序方向的“ASC”和“DESC”,然后在当对数据进行排序。

其次,一个翻转功能,将传递一个排序方向并将其翻转,当您第一次单击列时使用它,例如它会排序“ASC”,然后您第二次单击它时,它必须是“DESC”这就是我翻转它的原因。

第三,视图状态用于保留前一个排序的值。

第四,创建一个视图状态来保存 PageIndexChanging 事件的排序数据表,当然您必须检查视图状态是否为空,然后您必须获取数据并绑定它,否则您必须将网格视图与排序数据表绑定。

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";
    }
}
于 2012-06-01T08:23:05.737 回答