2012-10-17 2 views
0

고정 된 수의 행을 허용하는 사용자 정의 gridview를 작성하여 10 개의 항목 수 페이지에 2 행의 데이터 만 있으면 8 개의 여분의 빈 행이 생성됩니다. 데이터가 전혀없는 경우에도 작동합니다. 10 개의 빈 행을 생성합니다. 그것이해야합니다. 그 위대한데이터가없는 ASP.NET gridview에서 큰 빈 공간을 없애려면 어떻게해야합니까?

그러나 ... 데이터가 없다면 내 10 빈 행 아래에도이 거대한 빈 공간이 추가됩니다. 나는 데이터가 gridview에 바인딩되어 있지 않을 때 생성되어야하는 정상적인 공간을 믿지만, 나는 그것을 원하지 않는다. 그걸 어떻게 없앨 수 있니?

다음
 <asp:Panel ID="pnlGrdCustomers" runat="server" Width="100%" Height="100%" CssClass="srcColor">    
     <X:GridViewX ID="gvxTaskList" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDsbyStatus" 
      Width="100%" Height="100%" CssClass="tablestyle" OnRowCreated="gvxTaskList_RowCreated"> 
      <AlternatingRowStyle CssClass="altrowstyle" /> 
      <HeaderStyle CssClass="headerstyle" /> 
      <RowStyle CssClass="rowstyle" Wrap="false" /> 

      <EmptyDataRowStyle BackColor="#edf5ff" Height="300px" VerticalAlign="Middle" HorizontalAlign="Center" /> 
      <EmptyDataTemplate > 
      </EmptyDataTemplate> 
      <Columns> 
       <asp:BoundField DataField="TicketId" HeaderText="TicketId" SortExpression="TicketId" /> 
       <asp:BoundField DataField="TicketCreated" HeaderText="TicketCreated" SortExpression="TicketCreated" /> 
       <asp:BoundField DataField="ContactName" HeaderText="ContactName" ReadOnly="True" SortExpression="ContactName" /> 
       <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" ReadOnly="True" SortExpression="CompanyName" /> 
       <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" /> 
       <asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True" SortExpression="Status" /> 
       <asp:BoundField DataField="Priority" HeaderText="Priority" ReadOnly="True" SortExpression="Priority" /> 
       <asp:BoundField DataField="AssignedTo" HeaderText="AssignedTo" ReadOnly="True" SortExpression="AssignedTo" /> 
      </Columns> 
     </X:GridViewX> 
     <asp:SqlDataSource ID="SqlDSbyStatus" runat="server" ConnectionString="<%$ ConnectionStrings:EnterpriseConnectionString %>" SelectCommand="SELECT scT.TicketId, scT.TicketCreated, 
     (SELECT scCon.ContactName FROM scContacts scCon WHERE scCon.AccountId = scT.AccountId AND scT.ContactId = scCon.ContactId) AS 'ContactName', 
     (SELECT Name FROM scCompanies WHERE scT.AccountId = AccountId AND CompanyId = 
      (SELECT scCon.CompanyId FROM scContacts scCon WHERE scCon.AccountId = scT.AccountId AND scT.ContactId = scCon.ContactId)) AS 'CompanyName', 
     scT.Description, 
     (SELECT scStat.Description FROM scStatuses scStat WHERE scT.AccountId = scStat.AccountId AND scT.StatusId = scStat.StatusId) AS 'Status', 
     (SELECT scPri.Description FROM scPriorities scPri WHERE scT.AccountId = scPri.AccountId AND scT.PriorityId = scPri.PriorityId) AS 'Priority', 
     (SELECT (FirstName + ' ' + LastName) FROM Users WHERE scT.UserId = UserId) AS 'AssignedTo' 
     FROM scTickets scT 
     WHERE scT.AccountId = @1 AND StatusId = @2 Order By TicketCreated ASC"> 
      <SelectParameters> 
       <asp:Parameter Name="1" Type="Int64" /> 
       <asp:Parameter Name="2" Type="Int16"/> 
      </SelectParameters> 
     </asp:SqlDataSource> 

그것을 위해 뒤에 코드의 일부입니다 : 공공 부분 클래스 TestForm : System.Web.UI.Page { System.Configuration.Configuration webcfg 여기

는 마크 업의 일부입니다 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ("/ configuration");

protected void Page_Load(object sender, EventArgs e) 
    { 
     // Task List page loads with all open tickets 
     SqlDSbyStatus.SelectParameters.Clear(); 
     SqlDSbyStatus.SelectParameters.Add("1", TypeCode.Int64, "3"); 
     SqlDSbyStatus.SelectParameters.Add("2", TypeCode.Int64, "1"); 

     hidPageIndex.Value = gvxTaskList.PageIndex.ToString(); 
     hidRowCount.Value = gvxTaskList.unmodifiedRowCount.ToString(); 
     hidLastPage.Value = gvxTaskList.isLastPage.ToString(); 
    } 

    protected void gvxTaskList_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     string rowID = String.Empty; 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     {     
      rowID = "row" + e.Row.RowIndex; 
      e.Row.Attributes.Add("id", "row" + e.Row.RowIndex); 
      e.Row.Attributes.Add("onclick", "ChangeRowColor(" + "'" + rowID + "'" + ")");       
     } 
    } 

그리고 마지막으로 내가 오버라이드있는 gridview 코드 : 다음의 코드를 수정, 간단한 데이터가없는 메시지를 채우는 EmptyDataTemplate 태그를 활용하여 내 문제를 해결할 수 있었다

namespace GridViewX 
{ 
    [Description("Represents a custom GridView that creates additional empty rows to fill a fixed-row grid.")] 
    [ToolboxData("<{0}:GridViewX runat=server></{0}:GridViewX>")] 
public class GridViewX : System.Web.UI.WebControls.GridView 
{ 
    protected override void OnDataBound(EventArgs e) 
    { 
     GridViewRow gvRow = null; 

     isLastPage = (this.PageIndex + 1 == this.PageCount) ? true : false; 
     unmodifiedRowCount = this.Rows.Count; 

     for (int rows = this.Rows.Count; rows < this.PageSize; rows++) 
     { 
      gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); 

      for (int columns = 0; columns < this.Columns.Count; columns++) 
      { 
       gvRow.Controls.Add(new TableCell()); 
      } 

      //Inserts the rows right above the footer row. 
      //Remove the "- 1" if you are not using a footer. 
      this.Controls[0].Controls.AddAt(this.Controls[0].Controls.Count - 1, gvRow); 
     } 
    }  
} 
+0

우리가 코드의 일부를 볼 수 있습니다

<asp:GridView ID="GridView1" CssClass="MyGridView" runat="server">

다음 CSS에서 테이블이 원하는대로 패딩을 colums 변경? – Goose

+0

확인 코드가 추가되었습니다. 누구든지 아이디어가 있습니까? –

답변

0

protected override void OnDataBound(EventArgs e) 
{ 
    GridViewRow gvRow = null; 

    isLastPage = (this.PageIndex + 1 == this.PageCount) ? true : false; 
    unmodifiedRowCount = this.Rows.Count; 

    if(this.Rows.Count > 0) // Added only this if statement to fix my problem 
    { 
     for (int rows = this.Rows.Count; rows < this.PageSize; rows++) 
     { 
      gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); 

      for (int columns = 0; columns < this.Columns.Count; columns++) 
      { 
       gvRow.Controls.Add(new TableCell()); 
      } 

      this.Controls[0].Controls.AddAt(this.Controls[0].Controls.Count - 1, gvRow); 
     } 
    } 
}  
1

의 GridView의가 테이블로 렌더링되며, 간단한 변경을 테이블 colums 사이의 패딩 간격 삭제 : 사용자 정의의 gridview 그래서 같은 데이터가 없었다 있는지 확인하기 위해 검사를 할 수 있습니다. 먼저이의 GridView에 대한 주요 CSS를 이름을 만들 :

.MyGridView{ 
border: none; 

} 

.MyGridView tr{ 
    text-align: left; 
    vertical-align: top; 

} 

.MyGridView td{ 
    vertical-align: top; 
padding-bottom: 0px; 

    } 
관련 문제