如何在没有数据的情况下摆脱ASP.NET网格视图中的大空白空间?

时间:2012-10-17 22:51:39

标签: asp.net gridview

我构建了一个允许固定行数的自定义gridview,因此在10个条目计数页面上只有2行数据,它将生成8个额外的空行。即使根本没有数据,这也可以工作。它生成10个空行。正如它应该。很棒

但是......如果没有数据,它还会在我的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>

以下是它背后的一些代码:         public partial class TestForm:System.Web.UI.Page     {         System.Configuration.Configuration webcfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(&#34; / configuration&#34;);

    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代码:

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);
        }
    }    
}

2 个答案:

答案 0 :(得分:1)

GridView被渲染为表格,并且要删除间距,只需更改表格列之间的填充。首先为GridView创建一个主要的css名称:

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

然后在css中根据需要更改表colums padding:

.MyGridView{
border: none;

}

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

 }

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

  }

答案 1 :(得分:0)

我能够通过利用EmptyDataTemplate标记解决我的问题,用简单的无数据消息填充它,然后修改我的自定义gridview的代码以检查是否没有这样的数据:

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);
        }
    }
}