0

我构建了一个允许固定行数的自定义网格视图,因此在 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("/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代码:

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

2 回答 2

1

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

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

然后在 css 中根据需要更改表列填充:

.MyGridView{
border: none;

}

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

 }

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

  }
于 2013-02-10T07:35:45.533 回答
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);
        }
    }
}    
于 2012-10-18T15:56:01.677 回答