Gridview -Sorting事件不起作用II

时间:2013-09-14 15:11:19

标签: c# asp.net sorting gridview

我有一个可编辑的GridView,我正在尝试将排序功能添加到指定的列。虽然我没有收到错误,我的编辑/插入/删除功能按预期方式,但我的排序方法不起作用。我能不能在这里得到一些帮助?

设计:

<asp:GridView ID="gvLogNotice" 
        runat="server" 
        AutoGenerateColumns="false" 
        ShowFooter="false"
        OnRowCancelingEdit="gvLogNotice_RowCancelingEdit"
        OnRowEditing="gvLogNotice_RowEditing" 
        OnRowUpdating="gvLogNotice_RowUpdating"
        EmptyDataText="There are no data records to display."
        DataKeyNames="LogNoticeID"
        AllowPaging="true"
        AllowSorting="true"
        OnSorting="gvLogNotice_sorting"
        Width="700px">
        <Columns>
            <asp:TemplateField HeaderText="Log No." Visible="false">
                <ItemTemplate>
                    <%#Eval("LogNoticeID")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtLogNoticeID" runat="server" Enabled="false" Text=' <%#Eval("LogNoticeID") %>'></asp:TextBox>
                </EditItemTemplate>
             </asp:TemplateField>
            <asp:TemplateField HeaderText="Log Date" SortExpression="DateLogged">
                <ItemTemplate>
                    <%#Eval("DateLogged")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtDateLogged" runat="server" Text=' <%#Eval("DateLogged") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Log Description" HeaderStyle-Width="50px" sortexpression="LogNoticeDescript">
                <ItemTemplate>
                    <%#Eval("LogNoticeDescript")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtLogNoticeDescript" runat="server" Text=' <%#Eval("LogNoticeDescript") %>'></asp:TextBox>
                </EditItemTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Responsible Party" sortexpression="ResponsibleParty">
                <ItemTemplate>
                    <%#Eval("ResponsibleParty")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtResponsibleParty" runat="server" Text=' <%#Eval("ResponsibleParty") %>'></asp:TextBox>
                </EditItemTemplate>
          </asp:TemplateField>
             <asp:TemplateField HeaderText="Planned Date" SortExpression="PlannedDate" >
                <ItemTemplate>
                    <%#Eval("PlannedDate")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtPlannedDate" runat="server" Text=' <%#Eval("PlannedDate") %>'></asp:TextBox>
              </EditItemTemplate>
          </asp:TemplateField>
           <asp:TemplateField HeaderText="Case Number" SortExpression="CaseNumber">
                <ItemTemplate>
                    <%#Eval("CaseNumber")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCaseNumber" runat="server" Text=' <%#Eval("CaseNumber") %>'></asp:TextBox>
              </EditItemTemplate>
          </asp:TemplateField>
             <asp:TemplateField HeaderText="Log Status" SortExpression="LogStatus">
                <ItemTemplate>
                    <%#Eval("LogStatus")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtLogStatus" runat="server" Text=' <%#Eval("LogStatus") %>'></asp:TextBox>
                </EditItemTemplate>
          </asp:TemplateField>
            <asp:TemplateField HeaderText="Edit">
                <ItemTemplate>
                    &nbsp;&nbsp;
                    <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/edit.png" Width="25"
                        Height="25" CommandName="Edit" />&nbsp;&nbsp;
                   <%-- <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/img/delete.png" CommandName="Delete"
                        OnClientClick="return confirm('Are you sure want to delete record?')" />--%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update">Update</asp:LinkButton>&nbsp;&nbsp;
                    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                </EditItemTemplate>
              </asp:TemplateField>
        </Columns>
    </asp:GridView>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    lblmsg.Text = "";
    if (!Page.IsPostBack)
    {
        gvLogNotice.ShowFooter = false;
        //Load grid data using common method
        LoadGrid();
    }
}

//bind & load GV

void LoadGrid()
{
    sqlcmd = new SqlCommand("selectActiveLogs", sqlcon);
    sqlcmd.CommandType = CommandType.StoredProcedure;
    try
    {
        sqlcon.Open();
        da = new SqlDataAdapter(sqlcmd);
        dt.Clear();
        da.Fill(dt);
        gvLogNotice.DataSource = dt;
        gvLogNotice.DataBind();
    }
    catch (Exception ex)
    {
    }
    finally
    {
        sqlcon.Close();
    }
}

//sorting event

protected void gvLogNotice_sorting(object sender, GridViewSortEventArgs e)
{
    switch (e.SortExpression)
    {
        case "DateLogged":
            if (e.SortDirection == SortDirection.Ascending)
            {
                LoadGrid();
            }
            else
            {
                LoadGrid();
            }

            break;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要在LoadGrid中对表进行排序。

编辑:对于框架&lt; 4.0,添加一个重载方法,如下所示:

 void LoadGrid(string srtexpr, string direc)
{
    sqlcmd = new SqlCommand("selectActiveLogs", sqlcon);
    sqlcmd.CommandType = CommandType.StoredProcedure;
    try
    {
        sqlcon.Open();
        da = new SqlDataAdapter(sqlcmd);
        dt.Clear();
        da.Fill(dt);

        DataView dv = new DataView (dt);
        dv.Sort = srtexpr + " " + direc;
        gvLogNotice.DataSource = dv;
        gvLogNotice.DataBind();

    }
    catch (Exception ex)
    {

    }
    finally
    {
        sqlcon.Close();
    }
}

并通过排序条件:

protected void gvLogNotice_sorting(object sender, GridViewSortEventArgs e)
{
    switch (e.SortExpression)
    {
        case "DateLogged":
            if (e.SortDirection == SortDirection.Ascending)
            {
               LoadGrid("DateLogged", "ASC");
            }
            else
            {
                LoadGrid();
            }

            break;

       }

}