Gridview技巧6

前臺頁面:
一個gridview,4個button.分別爲:前一頁,下一頁,尾頁,首頁
cs代碼:

using System.Data.SqlClient;

public partial class Default10 : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {         if (!IsPostBack)         {             bind1();         }     }     public void bind1()     {         Button1.Enabled = true;         Button2.Enabled = true;         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString);

        try         {             conn.Open();             SqlDataAdapter dap = new SqlDataAdapter("select * from authors", conn);             DataSet ds = new DataSet();             dap.Fill(ds, "authors");             if (ds.Tables[0].Rows.Count == 0)             {                 AddDummyData(ds);             }             GridView1.DataSource = ds.Tables["authors"];             GridView1.AllowPaging = true;             GridView1.PageSize = 5;             GridView1.DataBind();             conn.Close();             if (GridView1.PageIndex == 0)             {                 Button1.Enabled = false;             }             if (GridView1.PageIndex == GridView1.PageCount - 1)             {                 Button2.Enabled = false;             }         }         catch         {

        }         finally         {             conn.Close();         }     }

    protected void Button1_Click(object sender, EventArgs e)     {         GridView1.PageIndex = GridView1.PageIndex - 1;         bind1();     }     protected void Button2_Click(object sender, EventArgs e)     {         GridView1.PageIndex = GridView1.PageIndex + 1;         bind1();     }

    protected void Button3_Click(object sender, EventArgs e)     {         GridView1.PageIndex = GridView1.PageCount - 1;         bind1();     }     protected void Button4_Click(object sender, EventArgs e)     {         GridView1.PageIndex = 0;         bind1();     }     private void AddDummyData(DataSet ds)     {

        DataTable dt = ds.Tables[0];

        DataRow newRow = dt.NewRow();

        dt.Rows.Add(newRow);

    }     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)     {         GridView1.PageIndex = e.NewPageIndex;         bind1();     } }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章