首页新闻找找看学习计划

请问在C#中DataGridview 中可以使用按钮来实现分页吗??

0
[待解决问题]

请问在C#中DataGridview 中可以使用按钮来实现分页吗??

本人菜鸟  求大神帮助  谢谢!!

C#
烂泥的主页 烂泥 | 初学一级 | 园豆:200
提问于:2012-03-12 18:42
< >
分享
所有回答(3)
0

public void fenye()
    {
        //string connstring = commDBHelper.connDb();
        //MySqlConnection conn = new MySqlConnection(connstring);
        //conn.Open();//打开数据库,连接
        string mysql = "select count(*) from Hospital ";
       // SqlCommand cmd = new SqlCommand(mysql, conn);
         DataTable dt=sqlcommDBHelper.ExecuteReader(mysql,CommandType.Text,null);
        int sum = Convert.ToInt32(dt.Rows[0][0].ToString ());//记录总数
        Label3.Text = sum.ToString();
        string mysql1 = "select * from Hospital ";
    
        //MySqlDataAdapter myda = new MySqlDataAdapter(mysql1, conn);
        DataSet ds = new DataSet();
        ds = sqlcommDBHelper.ExecuteDataSetReader(mysql1, CommandType.Text, null);

        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;//允许分页
        pds.PageSize = 6;
        pds.CurrentPageIndex = Convert.ToInt32(Label1.Text) - 1;//计算当前显示的页数
        contextDataGrid.DataSource = pds;
        Label2.Text = pds.PageCount.ToString();//显示总共多少页
        Label1.Text = (pds.CurrentPageIndex + 1).ToString();//显示当前页数
        this.LinkButton1.Enabled = true;
        this.LinkButton2.Enabled = true;
        this.LinkButton3.Enabled = true;
        this.LinkButton4.Enabled = true;
        if (pds.CurrentPageIndex < 1)
        {
            this.LinkButton1.Enabled = false;
            this.LinkButton2.Enabled = false;
        }
        if (pds.CurrentPageIndex == pds.PageCount - 1)
        {
            this.LinkButton3.Enabled = false;
            this.LinkButton4.Enabled = false;
        }
        contextDataGrid.DataBind();


    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Label1.Text = "1";
        fenye();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(Convert.ToInt32(Label1.Text) - 1);
        fenye();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(Convert.ToInt32(Label1.Text) + 1);
        fenye();
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        Label1.Text = Label2.Text;
        fenye();
    }

式圣2012 | 园豆:196 (初学一级) | 2012-03-13 09:27
0

private void DataListBind()
{
PagedDataSource pds = new PagedDataSource();
pds.AllowPaging = true; //实现分页功能
pds.CurrentPageIndex = CurrentPage; //使用状态保存当前页数
pds.DataSource = zd_unit_codeManagers.selectAllZd_unit_code();
pds.PageSize = 3; //每页显示数据条数
PageCount = pds.PageCount; //总共数据数量
this.Label1.Text = "第" + CurrentPage + "页,共" + (PageCount -1) + "页";

this.GridView1.DataSource = pds;
this.GridView1.DataBind();
buttonShowAndEnabled();


}

/// <summary>
/// 按钮可用不可用
/// </summary>
public void buttonShowAndEnabled()
{
if (CurrentPage <= 1)
{
this.btnUpPage.Enabled = false;
}
else
{
this.btnUpPage.Enabled = true;
}

if (CurrentPage >= PageCount - 1)
{
this.btnDownPage.Enabled = false;
}
else
{
this.btnDownPage.Enabled = true;
}

}

 

protected void btnUpPage_Click(object sender, EventArgs e)
{
CurrentPage--;
DataListBind();
}

protected void btnDownPage_Click(object sender, EventArgs e)
{
CurrentPage++;
DataListBind();
}

protected void btnUpPage_Click(object sender, EventArgs e)
{
CurrentPage--;
DataListBind();
}

protected void btnDownPage_Click(object sender, EventArgs e)
{
CurrentPage++;
DataListBind();
}

秋风sao落叶 | 园豆:44 (初学一级) | 2012-03-13 17:10
0

你可以看下 EasyCode .NEt 代码生成器 生成出来的界面有分页功能的,有源码的~~http://www.cnblogs.com/BudEasyCode/archive/2012/02/27/2370549.html#2327705

kubi程序员 | 园豆:205 (菜鸟二级) | 2012-03-14 21:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册