我构建了一个允许固定行数的自定义网格视图,因此在 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);
}
}
}