[英]custom paging for a GridView bound to an ObjectDataSource without using a stored procedure
[英]Data binding a gridview using ObjectDataSource without Stored Procedure
我有1000條記錄要顯示在網格視圖中,因此我正在使用ObjectDataSource綁定到網格視圖。
<asp:ObjectDataSource ID="odsProducts" runat="server" SelectMethod="GetProducts"
TypeName="ProductsList" EnablePaging="True" MaximumRowsParameterName="PageSize"
SelectCountMethod="GetRowsCount" StartRowIndexParameterName="StartRow"></asp:ObjectDataSource>
<asp:GridView ID="UserTable" runat="server" AllowPaging="True" DataSourceID="odsProducts"
SelectedIndex="0" DataKeyNames="UserID" ShowHeaderWhenEmpty="True" BorderStyle="Groove"
OnRowDataBound="UserTable_RowDataBound" AutoGenerateColumns="false">
在數據訪問層中,我有一個[Dataserver]
類來進行打開和關閉連接,我有一個稱為executeNonQuery方法的方法,該方法用於從數據庫中檢索數據。
public int ExecuteNonQuery(CommandType commandType, string sql, MySqlParameter[] commandParameters)
{
...
return rowsAffected;
}
所以在我的OjectdataSource的Getdata方法中
private DataView GetData(int StartRow, int PageSize)
{
DataServer server = new DataServer();
DataTable dt = new DataTable();
int newOrgID = 60;
string userQuery = "Select tbl_User.UserID, tbl_User.FirstName, tbl_User.LastName, tbl_User.Email, tbl_User.PhoneNumber, tbl_User.CreatedBy, tbl_Organisation.OrganisationName from tbl_user WHERE Organisationid=@OrganisationID order by tbl_user.LastName asc";
MySqlParameter[] para = new MySqlParameter[1];
para[0] = new MySqlParameter("@OrganisationID", MySqlDbType.Int32);
para[0].Value = newOrgID ;
dt = server.ExecuteQuery(CommandType.Text, userQuery, para);
}
網絡上的所有示例都帶有存儲過程,可以有人幫助我做到這一點。 我在這里遇到的錯誤是我無法綁定網格視圖,無法將數據集轉換為datable。 那么我該如何使用數據表綁定到gridview
只需更改您的SelectMethod
的簽名即可
private DataView GetData(int StartRow, int PageSize)
至
public DataTable GetProducts(MyNewParameter,StartRow,PageSize)
並在方法末尾返回dt
。
很好的例子: http : //www.codeproject.com/KB/aspnet/PagingWithODS.aspx
我如何使用數據表綁定到gridview
此示例說明如何使用DataTable作為數據源綁定到GridView:
MySqlCommand sm = new MySqlCommand("SELECT * FROM table", database_connection);
MySqlDataAdapter sa = new MySqlDataAdapter(sm);
DataTable dt = new DataTable();
sa.Fill(dt); // store query results in dt
gv.DataSource = dt;
gv.DataBind();
這也說明了如何在不使用存儲過程或ObjectDataSource的情況下執行選擇查詢。
如果您使用System.Data.SqlClient
而不是MySql.Data.MySqlClient
,則將類重命名為SqlCommand
和SqlDataAdapter
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.