2017-03-21 8 views
0

私は動的でページング機能を持つ共通のGridviewを持っています。行の数は、0から 'n'までの任意の数です。それがゼロのときは、<EmptyRow>セクションにLabelと表示されます。それは働いている。行番号に関係なく、Gridviewの高さを同じに保つ方法はありますか?

グリッドビューの高さを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; 
} 

私は20ピクセルで滞在するポケットベルライン、およびGridViewのを望みますブランクまたは空の行を持つ残りの行(存在する場合)を「パディング」します。

これは可能ですか?

私はテーブルを固定サイズのテーブル構造の中に収めたいからです。パネルを置き、

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

     </asp:Panel> 

おかげで以下のようなことに高さを与えるために

おかげ

答えて

0

私はそれを自分で解決しました。

ViewStateの機能を正確に使用する。

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

は、読み込み時のメソッドを追加します。まずそれを宣言

.. ..私は返すようにメソッドを作った= "GridView_Load"

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", ""); 
        } 
       } 
      } 
     } 
    } 

のOnLoadグリッドビューに空の行がある場合は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の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価値を失うということなので、それが失われる前に保存する必要があります。なぜこのようなことが起こるのかわかりませんが、ページングを処理するメソッドを生成すると、PageCountのデフォルト値は0になります。

だから、これは...私がいることをどのように処理するかを、私はGridViewコントロールごとに固有のViewStateを必要とする理由私は実際にそうしたがって、4 GridViews渡ってこれを処理しようとしています 注意です...

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を使用して、私はそれが便利な「新しい」値対「古い」扱いすることが判明し、私はとき時に呼び出すことができると述べている...ここにそのハンドルする必要がある。

空白の行を強制することによって、LASTページにページングしたときにグリッドビューの高さをそのまま維持していて、期待した10行未満である可能性があります。

MSは、このすべてを行う必要がないために、Persistence = "true"プロパティを提供する必要があります。たぶんVSのそれ以降のバージョンではこれが起こっていますが、私の場合はVS2005とASP.net 2. boo hooを使用しています。

私はこれが誰かを助けてくれることを願っています。

0

試してみます。

+0

ありがとうございますが、それだけでは不十分です。以前と同じようにGridviewが展開され、ページフッターが展開され、 "欠落している"行の間のスペースが埋まります。したがって、ページフッターは私が投稿した画像と同じように見えます – Fandango68

関連する問題