为什么我的网格视图不会将数据表排序为数据源

时间:2011-07-20 16:37:48

标签: c# asp.net ajax sorting gridview

问:

为什么我的gridview在单击列标题进行排序时不会做任何事情:

我的aspx :(在更新面板中)

<asp:GridView Width="100%" ID="gv_ActiveResearch" CssClass="datatable" AllowSorting="True"
                                                                runat="server" TabIndex="2" AutoGenerateColumns="False" AllowPaging="True" PageSize="5"
                                                                GridLines="None" OnRowDataBound="gv_ActiveResearch_RowDataBound" DataKeyNames="id"
                                                                OnPageIndexChanging="gv_ActiveResearch_PageIndexChanging" OnRowCommand="gv_ActiveResearch_RowCommand">
                                                                <EmptyDataTemplate>
                                                                    <table style="width: 100%;">
                                                                        <tr>
                                                                            <td>
                                                                            &nbsp;
                                                                        </tr>
                                                                        <tr>
                                                                            <td align="center">
                                                                                <asp:Label ID="Label4" runat="server" Font-Size="16pt" Text="&#1604;&#1575; &#1610;&#1608;&#1580;&#1583; &#1576;&#1610;&#1575;&#1606;&#1575;&#1578;"></asp:Label>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <td>
                                                                                &nbsp;
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </EmptyDataTemplate>
                                                                <Columns>
                                                                    <asp:TemplateField HeaderText="م">
                                                                        <ItemTemplate>
                                                                            <asp:Label ID="lblSerial" runat="server"></asp:Label>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField DataField="research_interest" HeaderText="research" SortExpression="research_interest">
                                                                        <ItemStyle HorizontalAlign="Left" />
                                                                    </asp:BoundField>
                                                                    <asp:TemplateField HeaderText="edit">
                                                                        <ItemTemplate>
                                                                            <asp:ImageButton ID="btn_Edit" runat="server" CausesValidation="False" ImageUrl="~/Images/editpen.png"
                                                                                CommandArgument='<%#((GridViewRow)Container).RowIndex%>' CommandName="editResearch" />
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:TemplateField HeaderText="delete">
                                                                        <ItemTemplate>
                                                                            <asp:ImageButton ID="btn_Delete" runat="server" CausesValidation="False" ImageUrl="~/Images/Symbols-Delete-icon.png"
                                                                                CommandArgument='<%#((GridViewRow)Container).RowIndex%>' CommandName="deleteResearch" />
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                </Columns>
                                                                <RowStyle VerticalAlign="Top" CssClass="row" />
                                                            </asp:GridView>

我的.cs:

protected void gv_ActiveResearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int index = Convert.ToInt32(e.CommandArgument);
                _activeResearchId = int.Parse(gv_ActiveResearch.DataKeys[index].Value.ToString());
                hf_activeResearchId.Value = _activeResearchId.ToString();
                if (e.CommandName == "editResearch")
                {
                    lbl_activeResearchSerial.Text = ((Label)gv_ActiveResearch.Rows[index].Cells[0].FindControl("lblSerial")).Text;
                    txt_activeResearch.Text = gv_ActiveResearch.Rows[index].Cells[1].Text.TrimEnd();
                    Ibtn_saveResearch.Visible = true;
                    Session["add_research"] = 1;

                }
                else if (e.CommandName == "deleteResearch")
                {
                    cc1lectresearchDTO obj = new cc1lectresearchDTO();
                    obj.Id = _activeResearchId;
                    int result = cc1lectresearchDAL.Delete(obj);
                    if (result > 0)
                    {
                        CancelResearch();
                        BindGV_ActiveResearch();
                        this.ShowStatus("The operation has been executed successfully.", "Note", "", "true");
                    }
                    else
                    {
                        this.ShowStatus("Wrong operation,Try again.", "Error", "error", "true");
                    }
                }

            }

            catch (Exception ee)
            {
                string message = ee.Message;
                this.ShowStatus("Wrong operation,Try again.", "Error", "error", "true");
            }
        }

private void BindGV_ActiveResearch()
        {
            try
            {
                gv_ActiveResearch.DataSource = cc1lectresearchDAL.List(int.Parse(Session["emp_num"].ToString()));//this method returns data table..
                gv_ActiveResearch.DataBind(); 
            }
            catch (Exception ee)
            {
                string message = ee.Message;
            }
        }

2 个答案:

答案 0 :(得分:2)

如果您有数据表作为数据源,则很容易排序。只需使用下面的内容并再次对数据进行数据绑定。

if (sortExpression != null && sortExpression.Length > 0) 
{
    table.DefaultView.Sort = sortExpression + " " + sortOrder;
}

查看this链接。

答案 1 :(得分:0)

您必须在需要排序的所有列中设置SortExpression="ColumnName"