.net中gridview控件数据绑定及分页

1.aspx页面内容

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"

            Height="174px" Width="572px" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="4" SelectedIndex="0">
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <PagerSettings Mode="NextPreviousFirstLast" NextPageText="&amp;gt;下一页" PreviousPageText="&amp;lt;上一页" />   //分页显示模式,可在控件属性那设置不同的风格

        </asp:GridView>

2.后台aspx.cs代码

 private DataView dv;   //全局变量,用来产生新的gridview控件覆盖原来的

 protected void Button1_Click(object sender, EventArgs e)
    {
        string paihao = TextBox1.Text.Trim();
        string constr = "server=localhost;database=YanChang_ZhS;uid=sa;pwd=sasa";
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlCommand com = new SqlCommand();
        com.Connection = con;
        string sql = "select riqi,paihao,pici,gongxu,jieguo from D_TCY where paihao='{0}'";
        string sql0 = string.Format(sql, paihao);
        com.CommandText = sql0;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = com;
        DataSet ds = new DataSet();
        da.Fill(ds);
        dv = ds.Tables[0].DefaultView;
        this.GridView1.DataSource = dv;
        this.GridView1.DataBind();
        con.Close();


        


    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;    //必须在数据绑定之前
        //GridView1.DataSource = dv;
        //GridView1.DataBind();
        string paihao = TextBox1.Text.Trim();
        string constr = "server=localhost;database=YanChang_ZhS;uid=sa;pwd=sasa";
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlCommand com = new SqlCommand();
        com.Connection = con;
        string sql = "select riqi,paihao,pici,gongxu,jieguo from D_TCY where paihao='{0}'";
        string sql0 = string.Format(sql, paihao);
        com.CommandText = sql0;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = com;
        DataSet ds = new DataSet();
        da.Fill(ds);
        dv = ds.Tables[0].DefaultView;
        this.GridView1.DataSource = dv;
        this.GridView1.DataBind();
        con.Close();


    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值