>>>>SqlDataReader r = sqlComm.ExecuteReader( CommandBehavior.SingleRow );
>>>>DataGrid1.DataSource = r;你使用的是DataReader,不可以实现分页,建议使用DataSet
>>>>DataGrid1.DataSource = r;你使用的是DataReader,不可以实现分页,建议使用DataSet
解决方案 »
- winform 自定义控件的方法怎么写?
- LsitBox保存数据的问题!!!!!!!!
- 怎样用javascript在c#中点删除按钮判断是否删除,以下语句错在哪里?
- 给分求助,为什么报表显示的数据有很多行,而执行同样的sql只有1行数据?
- 求一个能较好地处理类似名片的图片的二值化代码
- Farpoint Spread工具条怎么导出?
- 我想在一个TextBox控件里输入后当我点回车时就刷新一下界面.这个我应该怎么写呀?
- 如何把一个4字节的浮点数转换成十进制的数
- Word问题,急啊
- 思归等大侠,问个问题,能动态的生成一个Word文件吗?
- 如果 取得DataView 的所有行?
- 自動關機,進者給分,急用,在線等
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
try
{
myConnection.Open(); SqlDataReader r = sqlComm.ExecuteReader( CommandBehavior.SingleRow );
DataGrid1.DataSource = r;
DataGrid1.DataBind();
r.Close();
}
catch (Exception ex)
{
string cc = ex.Message;
}
finally
{
myConnection.Close();
} }
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server"> void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
// Databind the data grid on the first request only
// (on postback, rebind only in paging command)
BindGrid();
}
}
void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
void BindGrid() {
// TODO: update the ConnectionString and CommandText values for your application
string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
string CommandText = "select au_lname, au_fname, address, city, state from Authors order by au_lname";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Data Report with Paging
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" width="80%" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGrid_Page" PageSize="6" AllowPaging="true">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</form>
</body>
</html>
再给DataGrid控件添加PageIndexChanged事件;
代码为:
private void BindGride()
{
this.sqlConnection1.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["car"];
string CommandString = "";
CommandString = "select * from dpxxb order by datetime desc ";
this.sqlConnection1.Open ();
DataSet dataSet1=new DataSet ();
SqlDataAdapter myCommane = new SqlDataAdapter(CommandString,sqlConnection1);
myCommane.Fill(dataSet1,"tabmsg");
this.DataGrid1.DataSource = dataSet1.Tables["tabmsg"].DefaultView;
DataGrid1.DataBind();
this.sqlConnection1 .Close ();
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGride();
}