2011-07-20 4 views
1

Qとして、データテーブルでソートしていない理由:それを並べ替えるには、列のヘッダーをクリックしたときに私の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; 
      } 
     } 
+0

ソート方法をコーディングする必要があります。データを並べ替える方法はたくさんあります。 –

答えて

2

データソースとしてデータテーブルがある場合は、並べ替えが簡単です。以下のようなものを使用して、グリッドを再度データバインドしてください。

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

thisリンクをご覧ください。

+0

どこにこれを書いて、私は3つの列を持っていると私はそれを行う方法をそれぞれクリックするときにソートしたい場合。 –

+0

あなたは私が言及したリンクを見ましたか?これを確認してください。http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorting.aspx ..基本的に、各列にソート式を割り当て、並べ替えを処理するためのコード。イベントの並べ替えの式とソート順をチェックし(上記のリンクで説明したように)、データテーブルをソートして再度グリッドをバインドします。 –

0

並べ替えに必要なすべての列にSortExpression="ColumnName"を設定する必要があります。

+0

私は、 'SortExpression =" research_interest "'は 'aspx'を探しました。 –

関連する問題