2014-04-10 4 views
0

안녕하세요. "DataTable을 DataView를 사용하기 전에 설정해야합니다."에 대한 검색 결과를 살펴 보았습니다. 그러나 그들의 해결책 중 어느 것도 내 문제를 해결하거나 올바른 방향으로 나를 지적하지 못했습니다.DataTable을 사용하기 전에 DataTable을 설정해야합니다.

내가 여기 잘못 가고 있는데, 헤더를 눌러 gridview 테이블을 정렬하면 오류 및 정렬이 발생하지 않습니다. 오류는이 게시물의 내 ​​제목과 동일합니다.

편집 : 그 관련 확실하지만 난이 MS SQL 데이터베이스

마크 업

<asp:GridView ID="_propertyGridView" runat="server" CssClass="table table-hover table-striped" AutoGenerateColumns="false" AllowSorting="true" GridLines="None" OnRowCommand="PropertyRowCommand" Width="100%"> 
    <Columns> 
     <asp:BoundField DataField="Id" HeaderText="Ref" SortExpression="Id" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="PostCode" HeaderText="Post Code" SortExpression="PostCode" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="ContractsFinishedOn" HeaderText="Contract Signed On" SortExpression="ContractsFinishedOn" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="Mobile" HeaderText="Mobile No." SortExpression="Mobile" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="LandLordEmail" HeaderText="Owners Email" SortExpression="LandLordEmail" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:BoundField DataField="MoveInDate" HeaderText="Move In Date" SortExpression="MoveInDate" HeaderStyle-HorizontalAlign="Left" /> 
     <asp:TemplateField HeaderText="Status" SortExpression="Status1"> 
      <ItemTemplate> 
       <asp:CheckBox ID="_tenantPaymentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("TenantReceipt") %>' /> 
       <asp:LinkButton ID="_tenantPaymentLink" Text="Send Tenant Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="TenantEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the tenant, please make sure its correct');" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Status" SortExpression="Status2"> 
      <ItemTemplate> 
       <asp:CheckBox ID="_landlordInfoCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordInfo") %>' /> 
       <asp:LinkButton ID="_landlordInfoLink" Text="Request Landlord Info" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordInfoEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the landlord, please make sure its correct');" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Status" SortExpression="Status3"> 
      <ItemTemplate > 
       <asp:CheckBox ID="_landlordRentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordReceipt") %>' /> 
       <asp:LinkButton ID="_landlordRentLink" Text="Send Landlord Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the landlord, please make sure its correct');" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

코드 숨김

protected void _propertyGridView_Sorting(object sender, GridViewSortEventArgs e) 
{ 
    try 
    { 
     string sortExpression = e.SortExpression; 
     ViewState["z_sortexpresion"] = e.SortExpression; 
     if (GridViewSortDirection == SortDirection.Ascending) 
     { 
      GridViewSortDirection = SortDirection.Descending; 
      SortGridView(sortExpression, "DESC"); 
     } 
     else 
     { 
      GridViewSortDirection = SortDirection.Ascending; 
      SortGridView(sortExpression, "ASC"); 
     } 
    } 
    catch (Exception ex) 
    { 
     SearchErrorLbl.Text = "error in such"; 
    } 
} 

public SortDirection GridViewSortDirection 
{ 
    get 
    { 
     if (ViewState["sortDirection"] == null) 
      ViewState["sortDirection"] = SortDirection.Ascending; 
     return (SortDirection)ViewState["sortDirection"]; 
    } 
    set 
    { 
     ViewState["sortDirection"] = value; 
    } 

} 

private void SortGridView(string sortExpression, string direction) 
{ 
    DTSorting = new DataView(DTSorting, "", sortExpression + " " + direction, DataViewRowState.CurrentRows).ToTable(); 
    _propertyGridView.DataSource = DTSorting; 
    _propertyGridView.DataBind(); 
} 

public DataTable DTSorting 
{ 
    get 
    { 
     if (ViewState["Sorting"] != null) 
      return (DataTable)ViewState["Sorting"]; 
     else 
      return null; 
    } 
    set 
    { 
     ViewState["Sorting"] = value; 
    } 
} 
에서 페이지로드에있는 gridview를 채울 수 없음
+0

은'데이터-source'가 null 보인다 –

답변

0

문제가 SortGridView 방법 내에 있다고 보입니다. 동일한 DTSorting 속성을 사용하여 table 생성자 매개 변수로 설정하려고 시도합니다. 이 시점에서 null 인 경우는 예외를 throw합니다. DataView에는 작업 할 기존 DataTable 인스턴스가 필요합니다.

또한 현재 구현에서는 처리되지 않을 복수 DataTable 인스턴스를 생성 할 가능성이 높습니다. 한 가지 확실한 것은 여러분이 만든 테이블의 DataView 객체를 삭제하지 않기 때문에 정렬 된 테이블을 얻을 수 있다는 것입니다.

내가 당신이라면 데이터 바인딩 방법을 다시 생각해 보겠습니다. DataTable 개체를 ViewState에 저장하는 것은 좋지 않습니다. 전체 테이블은 클라이언트와 서버간에 앞뒤로 직렬화되어 성능이 저하됩니다.

난 당신이 여기에 대해 읽어 제안 : View State Overview

+0

감사하지만 그건 정말 내 문제 – Deviney

+0

@Deviney 당신이 DTSorting' 속성이'반환하지 않는다 '한다는 말을 수정하지 않습니다 null'? – Crono