How to: Bind Each Table of a DataSet to a Page on a GridView

FoxCompany

New Member
I've searched and searched, and I think this should be rather straight forward, but I can't figure it out. I have a DataSet (myDataSet) to which I have added several tables (myDataTable). I also have a GridView. I want to set the GridView up so that each page on the GridView is bound to each each table of the DataSet (i.e. page 1 of GridView = DataSet.Table[0], etc.). The tables all have the same columns, but differing number of rows. With PageSize = number of rows in the table, the buttons disappear completely even though I have explicitly set PageButtonCount to 3 (there are 3 tables in this dataset). I suppose there's some sort of automagical paging feature that's determining that no more pages are needed but I can't sort out how to make it work.\[code\]public partial class _Default : System.Web.UI.Page{ Users theUsers = new Users(); static DataSet myDataSet = new DataSet(); DataTable myDataTable = new DataTable(); protected void Page_Load(object sender, EventArgs e) { protected bool runReport() { ltError.Visible = false; try { using (MySqlConnection dbConn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AsteriskConn"].ConnectionString)) { //sql query string sql = "SELECT calldate, src, dst, duration FROM cdr WHERE (src LIKE @ext AND dst > 9999)" + "OR (dst LIKE @ext AND src > 9999) AND dst NOT LIKE '*%'"; MySqlDataAdapter adapter = new MySqlDataAdapter(); dbConn.Open(); myDataSet.Tables.Clear(); int j = ddlUsers.SelectedIndex; //loop through each user in the list and query the database for its cdr records with the sql query string foreach (int i in theUsers.groups[j].extensions) { myDataTable = new DataTable(); adapter.SelectCommand = new MySqlCommand(sql, dbConn); adapter.SelectCommand.Parameters.Add("@ext", MySqlDbType.VarChar, 80).Value = http://stackoverflow.com/questions/14045830/i; adapter.Fill(myDataTable); myDataTable.TableName = i.ToString(); myDataSet.Tables.Add(myDataTable); } gvReport.DataSource = myDataSet; gvReport.AllowPaging = true; gvReport.PageSize = myDataSet.Tables[0].Rows.Count; gvReport.PagerSettings.PageButtonCount = myDataSet.Tables.Count; gvReport.PagerSettings.Mode = PagerButtons.NumericFirstLast; gvReport.PagerSettings.Position = PagerPosition.Top; gvReport.PagerSettings.Visible = true; gvReport.DataBind(); dbConn.Close(); } } }}protected void gvReport_PageIndexChanging(object sender, GridViewPageEventArgs e){ gvReport.PageIndex = e.NewPageIndex; gvReport.DataSource = myDataSet.Tables[e.NewPageIndex]; gvReport.DataBind();}<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center"> <asp:GridView ID="gvReport" runat="server" OnPageIndexChanging="gvReport_PageIndexChanging" onrowdatabound="gvReport_RowDataBound" Visible="False" HorizontalAlign="Center" AllowPaging="true"> <PagerSettings Position="Top" /> </asp:GridView> <asp:Literal ID="ltError" runat="server" Visible="False"></asp:Literal> </asp:Panel></asp:Content>\[/code\]
 
Back
Top