GridView控制項筆記

來源:互聯網
上載者:User
1、無資料依然顯示表頭

    // 參數:所要綁定的GridView      所要繫結資料集  
    public void BindNoRecords(GridView gridView, DataSet ds)
    {
        if(ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gridView.DataSource = ds;
            gridView.DataBind();
            int columnCount = gridView.Rows[0].Cells.Count;
            gridView.Rows[0].Cells.Clear();
            gridView.Rows[0].Cells.Add(new TableCell());
            gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
            gridView.Rows[0].Cells[0].Text = "沒有任何記錄!";
            gridView.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
        }
    }

2、BoundField的格式化字串

     DataFormatString="{0:F}" 還要加上HtmlEncode="false"才會生效。
     至於{0:F}格式請參照 String.Format格式說明

3、分頁

     將AllowPaging屬性設定為true,pagesize設定成你要求每頁顯示的行數,在pagerSettings中設定分頁樣式
     如  :
     FirstPageText:首頁
     LastPageText:尾頁
     NextPageText:下一頁
     PreviousPageText:上一頁
     
     Mode:NextPreviousFirstLast

     給GridView1添加GridView1_PageIndexChanging事件
     寫入代碼如:
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (GridView1.EditIndex != -1)
        {
            e.Cancel = true;
        }
        else
        {
            GridView1.PageIndex = e.NewPageIndex;
            dataBind();
        }
    }
    註:dataBind();是你綁定資料的函數,接觸過GridView的程式員都應該知道怎麼寫吧。

    這樣一個有分頁功能的gridview就實現了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.