Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保持Gridview';不管行数是多少,它的高度都一样吗?_C#_Html_Css_Asp.net_Gridview - Fatal编程技术网

C# 如何保持Gridview';不管行数是多少,它的高度都一样吗?

C# 如何保持Gridview';不管行数是多少,它的高度都一样吗?,c#,html,css,asp.net,gridview,C#,Html,Css,Asp.net,Gridview,我有一个通用的Gridview,它是动态的,并且具有分页功能。行数可以是0到“n”之间的任意值。当它为零时,我只需在部分显示一个标签。这是有效的 我想知道如何将gridview的高度设置为800px,而不使其延伸到寻呼机线路 我尝试了PagerStyle标签上的高度标签,但没有区别 <asp:GridView ID="gvFTUNSENT" runat="server" AutoGenerateColumns="False" Cel

我有一个通用的Gridview,它是动态的,并且具有分页功能。行数可以是0到“n”之间的任意值。当它为零时,我只需在
部分显示一个
标签。这是有效的

我想知道如何将gridview的高度设置为800px,而不使其延伸到寻呼机线路

我尝试了PagerStyle标签上的高度标签,但没有区别

            <asp:GridView ID="gvFTUNSENT" runat="server" 
                AutoGenerateColumns="False" CellPadding="4" ForeColor="Black" AllowSorting="True" CssClass="gvCSS" Height="800px"
                DataKeyNames="StudentID,StudentUnitID" DataSourceID="sdsFTUNSENT" 
                GridLines="Vertical" AllowPaging="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" >
                <RowStyle Wrap="true" Height="48px" />
                <PagerStyle Height="20px" />
                <Columns>
我希望寻呼机行保持在20px,Gridview用空格或空行“填充”重新标记的行(如果有)

可能吗

原因是我希望表能够装入固定大小的表结构中


谢谢

试着把面板放好,并像下面那样给它加上高度

<asp:Panel runat="server" ID="pnlGrid" Height="500px" ScrollBars="Auto">

        </asp:Panel>

谢谢。

我自己解决了

使用ViewState的强大功能来实现精确性

首先申报

if (!Page.IsPostBack)
{
    ViewState["DataSourceID"] = string.Empty;
}
添加一个OnLoad方法

OnLoad=“GridView\u加载”

现在,分页必须由代码处理。这就是我所做的。 请注意,页面更改后,我会检查它是否是gridview中的最后一个页面,并应用逻辑创建“空白”数据行,以便gridview的高度不会更改

protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gv = (GridView)sender;
    gv.PageIndex = e.NewPageIndex;
    gv.DataBind();
}

protected void GridView_PageIndexChanged(object sender, EventArgs e)
{
    //here we control gridviews that have more than 10 rows so we can paginate and fill in rows like
    //we did above in GridView_Load(). But it could not be done there because we're adding more rows
    //to a gridview that has not had it's paging reformatted! We're doing so here

    GridView gv = (GridView)sender;

    //padd with blank rows to make up a full gridview of PAGESIZE (default 10) rows per page
    DataSourceSelectArguments dss = new DataSourceSelectArguments();

    //get the datasource related to the gridview
    string wsDataSourceID = (gv.DataSourceID == string.Empty) ? ViewState["DataSourceID"].ToString() : gv.DataSourceID;
    SqlDataSource sds = (SqlDataSource)pnlMAIN.FindControl(wsDataSourceID);
    if (sds != null)
    {
        //load the data again but this time into a dataview object
        DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
        if (dv != null)
        {
            //convert the dataview to a datatable so we can see the row(s)
            DataTable dt = (DataTable)dv.ToTable();
            if (dt != null)
            {
                //have we reached the LAST page?
                if ((gv.PageIndex + 1) == ((gv.PageCount == 0) ? Convert.ToInt16(ViewState[wsDataSourceID + "PageCount"].ToString()) : gv.PageCount))
                {
                    //determines actual number of rows on the last page
                    int lastPageRowCount = dt.Rows.Count % gv.PageSize;
                    if (lastPageRowCount < gv.PageSize)
                    {
                        //loop through any "gap rows" and add empty rows
                        for (int wsRowAdd = (lastPageRowCount + 1); wsRowAdd <= gv.PageSize; wsRowAdd++)
                        {
                            DataRow dr = dt.NewRow();
                            dt.Rows.Add(dr);
                        }

                        //accept the new rows
                        dt.AcceptChanges();
                    }
                }

                //reload the datatable back to the gridview (either with extra rows, or the original data to not stuff up the paging)
                gv.DataSource = dt;
                if (gv.DataSourceID != string.Empty)
                    ViewState["DataSourceID"] = gv.DataSourceID;
                gv.DataSourceID = null;
                gv.DataBind();
            }
        }
    }
}
最后,我需要不断更新DataSourceID的ViewState,以便可以如上所示重新加载它

我在这里处理

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView gv = (GridView)sender;

        //update SourceID into memory, if it exists
        if (gv.DataSourceID != string.Empty)
            ViewState["DateSourceID"] = gv.DataSourceID;
}
这很冗长,但是在整个程序中使用ViewState,我发现处理“旧”与“新”的值和状态非常方便,我可以在需要时调用这些值和状态

因此,通过强制使用空白行,我在分页到最后一页时保持了gridview的高度不变,很可能少于我预期的10行

MS应该提供Persistence=“true”属性,以避免执行所有这些操作。也许在VS的更高版本中出现了这种情况,但在我的例子中,我使用的是VS2005和ASP.net 2。呜呜


我希望这对某人有所帮助。

谢谢,但这还不够。Gridview像以前一样展开,页脚现在展开以填充“缺失”行之间的空间。所以页脚看起来和我发布的图片一样
//checks if a gridview has any actual rows of data (not just blank rows filled in by the Load below
protected bool gvNoData(GridView gv)
{
    int wsDataRow = 0;
    foreach (GridViewRow gvRow in gv.Rows)
        if (gvRow.RowType == DataControlRowType.DataRow)
        {
            Label lblStudentName = (Label)gvRow.FindControl("lblStudentName");
            if (lblStudentName != null)
                if (lblStudentName.Text.Length > 0)
                    wsDataRow += 1;
        }

    //if a count was generated then there are data rows, otherwise the rows are blank or nonexistant
    if (wsDataRow > 0) return false;
    else return true;
}
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView gv = (GridView)sender;
    gv.PageIndex = e.NewPageIndex;
    gv.DataBind();
}

protected void GridView_PageIndexChanged(object sender, EventArgs e)
{
    //here we control gridviews that have more than 10 rows so we can paginate and fill in rows like
    //we did above in GridView_Load(). But it could not be done there because we're adding more rows
    //to a gridview that has not had it's paging reformatted! We're doing so here

    GridView gv = (GridView)sender;

    //padd with blank rows to make up a full gridview of PAGESIZE (default 10) rows per page
    DataSourceSelectArguments dss = new DataSourceSelectArguments();

    //get the datasource related to the gridview
    string wsDataSourceID = (gv.DataSourceID == string.Empty) ? ViewState["DataSourceID"].ToString() : gv.DataSourceID;
    SqlDataSource sds = (SqlDataSource)pnlMAIN.FindControl(wsDataSourceID);
    if (sds != null)
    {
        //load the data again but this time into a dataview object
        DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
        if (dv != null)
        {
            //convert the dataview to a datatable so we can see the row(s)
            DataTable dt = (DataTable)dv.ToTable();
            if (dt != null)
            {
                //have we reached the LAST page?
                if ((gv.PageIndex + 1) == ((gv.PageCount == 0) ? Convert.ToInt16(ViewState[wsDataSourceID + "PageCount"].ToString()) : gv.PageCount))
                {
                    //determines actual number of rows on the last page
                    int lastPageRowCount = dt.Rows.Count % gv.PageSize;
                    if (lastPageRowCount < gv.PageSize)
                    {
                        //loop through any "gap rows" and add empty rows
                        for (int wsRowAdd = (lastPageRowCount + 1); wsRowAdd <= gv.PageSize; wsRowAdd++)
                        {
                            DataRow dr = dt.NewRow();
                            dt.Rows.Add(dr);
                        }

                        //accept the new rows
                        dt.AcceptChanges();
                    }
                }

                //reload the datatable back to the gridview (either with extra rows, or the original data to not stuff up the paging)
                gv.DataSource = dt;
                if (gv.DataSourceID != string.Empty)
                    ViewState["DataSourceID"] = gv.DataSourceID;
                gv.DataSourceID = null;
                gv.DataBind();
            }
        }
    }
}
protected void GridView_DataBound(object sender, EventArgs e)
{
    GridView gv = (GridView)sender;

    //keep this because handing the last page and filling in rows, for some reason the PageCount is 0! And when it is, the DataSourceID could be null!
    //There should be 4 ViewStates to keep the PageCount of each GridView:
    //  1.  ViewState["sdsFTUNSENTPageCount"]
    //  2.  ViewState["sdsFTRESENDPageCount"]
    //  3.  ViewState["sdsASBAUNSENTPageCount"]
    //  4.  ViewState["sdsASBARESENDPageCount"]
    //  
    //  Each one will keep a PageCount value so that we don't lose it by using PageCount alone. And this is required so that we can "padd out"
    //  EmptyRows in the gridview when it's on the last page, giving the spreadsheet that clean un-shrinking look

    if (gv.PageCount != 0)
        ViewState[((gv.DataSourceID == string.Empty) ? ViewState["DataSourceID"].ToString() : gv.DataSourceID) + "PageCount"] = gv.PageCount;
}
    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView gv = (GridView)sender;

        //update SourceID into memory, if it exists
        if (gv.DataSourceID != string.Empty)
            ViewState["DateSourceID"] = gv.DataSourceID;
}