public void Page_Load(Object sender, EventArgs e) { // Initialize only the first time... if (!Page.IsPostBack) { lblURL.Text = Request.Url + "<hr>"; SetVirtualItemCount(); } }SqlDataReader dr; private SqlDataReader CreateDataSource(int nPageIndex) { // page index is assumed to be 0-based int nPageSize = grid.PageSize; int nBaseProductID = nPageSize * nPageIndex; // Set up the connection String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=sa;"; SqlConnection conn = new SqlConnection(strConn); // Set up the command String strCmd = "SELECT TOP " + nPageSize + " " + "productid, productname, quantityperunit, unitsinstock " + "FROM products " + "WHERE productid >" + nBaseProductID; SqlCommand cmd = new SqlCommand(strCmd, conn);
// Execute the command conn.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return dr; }public void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) { grid.CurrentPageIndex = e.NewPageIndex; grid.DataSource = CreateDataSource(grid.CurrentPageIndex); grid.DataBind(); dr.Close(); }public void OnLoadData(Object sender, EventArgs e) { grid.DataSource = CreateDataSource(0); grid.DataBind(); dr.Close(); }public void SetVirtualItemCount() { // Set up the connection String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=sa;"; SqlConnection conn = new SqlConnection(strConn); // Set up the command String strCmd = "SELECT COUNT(*) FROM products"; SqlCommand cmd = new SqlCommand(strCmd, conn);
// Execute the command conn.Open(); int nItemCount = (int) cmd.ExecuteScalar(); conn.Close(); grid.VirtualItemCount = nItemCount; return; }
仔细再看了一遍程序,发现把 dongbeiren() 朋友的SetVirtualItemCount()过程改写以后放进我的程序就对了。 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then SetVirtualItemCount() End If End Sub Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click Response.Redirect("htmlpage1.htm") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strSQL As String Dim Strbt = Trim(txtBT.Text) Dim Strnr = Trim(txtNR.Text) If Strnr = "" And Strbt = "" Then strSQL = "select * from files order by id desc" ElseIf Strbt = "" Then strSQL = "select * from files where texts like '%" & Strnr & "%' order by id desc" ElseIf Strnr = "" Then strSQL = "select * from files where topic like '%" & Strbt & "%' order by id desc" Else strSQL = " select * from files where topic like '%" & Strbt & "%' and texts like '%" & Strnr & "%' order by id desc" End If Label5.Text = strSQL datafiller() End Sub Sub DataFiller() Dim StrConn As String = ConfigurationSettings.AppSettings("DataConn") Dim objConn As New SqlConnection(StrConn) objConn.Open() Dim strSQL As String = Label5.Text Dim objAdapter As New SqlDataAdapter(strSQL, objConn) Dim objDataSet As New DataSet() objAdapter.Fill(objDataSet, "topic") MyGrid.DataSource = objDataSet.Tables.Item("topic") MyGrid.DataBind() End Sub Sub GridPageChange(ByVal S As Object, ByVal E As DataGridPageChangedEventArgs) MyGrid.CurrentPageIndex = E.NewPageIndex DataFiller() End Sub Sub SetVirtualItemCount() ' Set up the connection Dim strConn As String = ConfigurationSettings.AppSettings("DataConn") Dim conn As New SqlConnection(strConn) ' Set up the command Dim strCmd As String = "SELECT COUNT(*) FROM files" Dim cmd As SqlCommand = New SqlCommand(strCmd, conn) ' Execute the command conn.Open() Dim nItemCount As Int32 = cmd.ExecuteScalar() conn.Close() MyGrid.VirtualItemCount = nItemCount End Sub
{
// Initialize only the first time...
if (!Page.IsPostBack)
{
lblURL.Text = Request.Url + "<hr>";
SetVirtualItemCount();
}
}SqlDataReader dr;
private SqlDataReader CreateDataSource(int nPageIndex)
{
// page index is assumed to be 0-based
int nPageSize = grid.PageSize;
int nBaseProductID = nPageSize * nPageIndex;
// Set up the connection
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=sa;";
SqlConnection conn = new SqlConnection(strConn); // Set up the command
String strCmd = "SELECT TOP " + nPageSize + " " +
"productid, productname, quantityperunit, unitsinstock " +
"FROM products " +
"WHERE productid >" + nBaseProductID;
SqlCommand cmd = new SqlCommand(strCmd, conn);
// Execute the command
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}public void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
grid.CurrentPageIndex = e.NewPageIndex;
grid.DataSource = CreateDataSource(grid.CurrentPageIndex);
grid.DataBind();
dr.Close();
}public void OnLoadData(Object sender, EventArgs e)
{
grid.DataSource = CreateDataSource(0);
grid.DataBind();
dr.Close();
}public void SetVirtualItemCount()
{
// Set up the connection
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=sa;";
SqlConnection conn = new SqlConnection(strConn); // Set up the command
String strCmd = "SELECT COUNT(*) FROM products";
SqlCommand cmd = new SqlCommand(strCmd, conn);
// Execute the command
conn.Open();
int nItemCount = (int) cmd.ExecuteScalar();
conn.Close(); grid.VirtualItemCount = nItemCount;
return;
}
你的意思是通过查询得到的结果要首先给VirtualItemCount这个属性赋值?
奇怪我做的第一个程序怎么不用这么做呢,他只是列出全部的记录,而查询的结果也有可能是全部记录啊,但是出错了。
你的程序是用C#做的,有点看不明白,但是还是很感谢你。
能不能再说明白点,VirtualItemCount这个属性是起什么作用的?
html:
<asp:DataGrid ID="datagridReportInfo" BorderColor="#A285C9" AutoGenerateColumns="False" AllowPaging="True" Runat="server">
后台:
//分页处理
private void loadReplist_MRR()
{
try
{
string strUserID = Session["User"].ToString().Trim();
string strUserType = Session["UserType"].ToString().Trim(); MRRReport_Interface objload = new MRRReport_Interface();
DataSet dsReportList = objload.getReportList(strUserID,strUserType,"MRR");
DataView dvReportList = dsReportList.Tables[0].DefaultView;
dvReportList.Sort = "RMICDATE DESC";
this.datagridReportInfo.DataSource = dvReportList; int pagesum;
if(dvReportList.Count % datagridReportInfo.PageSize == 0)
pagesum = dvReportList.Count/datagridReportInfo.PageSize;
else
pagesum = dvReportList.Count/datagridReportInfo.PageSize + 1;
if(dvReportList.Count == 0)
pagesum = 1;
if(datagridReportInfo.CurrentPageIndex >= pagesum)
datagridReportInfo.CurrentPageIndex = pagesum - 1; this.datagridReportInfo.DataBind(); }
catch(Exception er)
{
throw er;
}
}
//实现分页
private void datagridReportInfo_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
try
{
datagridReportInfo.EditItemIndex = -1;
datagridReportInfo.CurrentPageIndex = e.NewPageIndex;
loadReplist_MRR();
}
catch(Exception sysErr)
{
ShowErrMessage(sysErr);
}
}
我照你的程序改写了自己的东西,可是没有用,还是出同样的提示,好像SetVirtualItemCount()这个过程没有起作用一样。
“选定的数据源不实现 ICollection ”是什么意思?
“选定的数据源不实现 ICollection ”是什么意思?
[email protected]
谢谢你的程序,不过不好意思——我看不太懂……
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SetVirtualItemCount()
End If
End Sub Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Response.Redirect("htmlpage1.htm")
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strSQL As String
Dim Strbt = Trim(txtBT.Text)
Dim Strnr = Trim(txtNR.Text) If Strnr = "" And Strbt = "" Then
strSQL = "select * from files order by id desc"
ElseIf Strbt = "" Then
strSQL = "select * from files where texts like '%" & Strnr & "%' order by id desc"
ElseIf Strnr = "" Then
strSQL = "select * from files where topic like '%" & Strbt & "%' order by id desc"
Else
strSQL = " select * from files where topic like '%" & Strbt & "%' and texts like '%" & Strnr & "%' order by id desc"
End If
Label5.Text = strSQL
datafiller() End Sub Sub DataFiller()
Dim StrConn As String = ConfigurationSettings.AppSettings("DataConn")
Dim objConn As New SqlConnection(StrConn)
objConn.Open() Dim strSQL As String = Label5.Text Dim objAdapter As New SqlDataAdapter(strSQL, objConn) Dim objDataSet As New DataSet()
objAdapter.Fill(objDataSet, "topic") MyGrid.DataSource = objDataSet.Tables.Item("topic")
MyGrid.DataBind()
End Sub Sub GridPageChange(ByVal S As Object, ByVal E As DataGridPageChangedEventArgs)
MyGrid.CurrentPageIndex = E.NewPageIndex
DataFiller()
End Sub Sub SetVirtualItemCount()
' Set up the connection
Dim strConn As String = ConfigurationSettings.AppSettings("DataConn")
Dim conn As New SqlConnection(strConn) ' Set up the command
Dim strCmd As String = "SELECT COUNT(*) FROM files"
Dim cmd As SqlCommand = New SqlCommand(strCmd, conn) ' Execute the command
conn.Open()
Dim nItemCount As Int32 = cmd.ExecuteScalar()
conn.Close() MyGrid.VirtualItemCount = nItemCount
End Sub