我已经搜索和搜索,我认为这应该是相当直截了当的,但我无法弄清楚。我有一个数据集 (myDataSet),向其中添加了几个表 (myDataTable)。我也有一个 GridView。我想设置 GridView 以便 GridView 上的每个页面都绑定到 DataSet 的每个表(即 GridView 的第 1 页 = DataSet.Table[0] 等)。这些表都有相同的列,但行数不同。
使用 PageSize = 表中的行数,即使我已将 PageButtonCount 显式设置为 3(此数据集中有 3 个表),按钮也会完全消失。我想有某种自动分页功能可以确定不需要更多页面,但我无法弄清楚如何使其工作。
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 = 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>