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="&gt;下一页" PreviousPageText="&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();
}