0

固定数の行を許可するカスタムグリッドビューを作成したので、10エントリカウントのページには2行のデータしかなく、8つの余分な空の行が生成されます。これは、データがまったくない場合でも機能します。10個の空の行を生成します。必要に応じて。それは素晴らしい

しかし...データがない場合は、10行の空の行の下にこの巨大な空のスペースも追加されます。グリッドビューにデータがバインドされていないときに生成されるはずの通常のスペースだと思いますが、必要ありません。どうすればそれを取り除くことができますか?

マークアップの一部は次のとおりです。

        <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>

その背後にあるコードの一部を次に示します。publicpartialclassTestForm: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 + "'" + ")");                           
        }
    }

そして最後に、私が上書きしたグリッドビューコード:

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 タグを利用して単純なデータなしメッセージを入力し、カスタム グリッドビューのコードを変更して次のようなデータがないかどうかを確認することで、問題を解決できました。

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 に答える