无论行号如何,如何保持Gridview的高度相同?

时间:2017-03-21 00:17:13

标签: c# html css asp.net gridview

我有一个动态的常见Gridview,并具有分页功能。行数可以是0到'n'之间的任何值。当它为零时,我只在Label部分显示<EmptyRow>。这是有效的。

我想知道如何将网格视图的高度设置为800px,而不会延长传呼线。

我在PagerStyle标签上尝试过Height标签,但没有区别。

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

我的Gridview看起来总是这样......

enter image description here

我的CSS ......

table.gvCSS {
  margin-top: 2px;
  font: 12px Verdana;
}

table.gvCSS > tbody > tr {
  background-color: white;
}

table.gvCSS > tbody > tr:nth-of-type(odd) {
  background-color: #EEE;
}

table.gvCSS > tbody > tr:first-of-type {
  background-color: #5D7B9D;
  color: white;
}

table.gvCSS > tbody > tr.selected-row {
  background-color: lightgreen;
}

table.gvCSSsub > tbody > tr.selected-row {
  background-color: lightyellow;
}

table.gvCSS tr td {
  padding: 3px 6px;
}

我希望寻呼机线保持在20px,并且Gridview要用空白或空行“填充”剩余的行(如果有的话)。

这可能吗?

原因是我希望表适合固定大小的表结构。

由于

2 个答案:

答案 0 :(得分:0)

尝试放置面板并给出高度,如下所示

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

        </asp:Panel>

感谢。

答案 1 :(得分:0)

我自己解决了。

使用ViewState的强大功能。

首先声明它..

if (!Page.IsPostBack)
{
    ViewState["DataSourceID"] = string.Empty;
}

添加OnLoad方法..

  

的OnLoad =&#34; GridView_Load&#34;

    protected void GridView_Load(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;

        DataSourceSelectArguments dss = new DataSourceSelectArguments();

        //get the datasource related to the gridview
        SqlDataSource sds = (SqlDataSource)pnlMAIN.FindControl(gv.DataSourceID.ToString());
        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)
                {
                    //padd with blank rows to make up a full gridview of PAGESIZE (default 10) rows per page
                    if (gv.Rows.Count < gv.PageSize)
                    {
                        //and this is necessary to not stuff up gridviews with data beyond PageSize rows. Otherwise we'll have to handle the paging in code!
                        if (dt.Rows.Count <= gv.PageSize)
                        {
                            //loop through any "gap rows" and add empty rows
                            for (int wsRowAdd = (gv.Rows.Count + 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
                            gv.DataSource = dt;
                            if (gv.DataSourceID != string.Empty)
                                ViewState["DataSourceID"] = gv.DataSourceID;
                            gv.DataSourceID = null;
                            gv.DataBind();
                        }
                    }

                    //hide gridview and reveal "emptyrow" label if there is no data to display
                    GridViewRow gHDR = (GridViewRow)gv.HeaderRow;
                    CheckBox cbHDR = (CheckBox)gHDR.FindControl("cbHDR");
                    if (gvNoData(gv))
                    {
                        gv.Visible = false;
                        Label lblEmpty = new Label();
((Label)pnlMAIN.FindControl("lblEmptyRow")).Style.Add("display", "");
                    }
                }
            }
        }
    }

如果gridview有任何&#34;空&#34;我已经创建了一个返回true或false的方法。行...

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

现在,分页必须由代码处理。这就是我的所作所为。 请注意,在页面更改后,我会检查它是否是gridview中的最后一页,并应用逻辑来创建&#34;空白&#34;数据行使Gridview的 HEIGHT 没有改变。

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

现在所有这些中的问题是,我们丢失了gridview中的PageCount 值,因此必须先将其保存在&#39;之前。输了。我不知道为什么会发生这种情况,但ASP必须看到,一旦你生成了处理分页的方法,它就会将PageCount默认为0。

所以这就是我处理的方式...... 请注意,我实际上是尝试在4个GridView中处理这个问题,因此我为每个gridview需要一个独特的ViewState ...

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

最后,我需要不断更新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我觉得它很方便处理&#34; old&#34; vs&#34; new&#34;我需要时可以调用的值和状态。

因此,通过强制空行,我在分页到LAST页面时保持了gridview的高度,并且很可能只有我预期的10行。

MS应该提供Persistence =&#34; true&#34;财产,以避免必须做所有这一切。也许在VS的后期版本中已经出现,但在我的情况下,我使用的是VS2005和ASP.net 2. boo hoo。

我希望这有助于某人。