none
弱弱的问个问题,gridview分页 RRS feed

  • 问题

  • 问题是这样的:gridview不绑数据源控件就不能用Numeric进行分页?
    我试了下,绑的是dataset显示都正确。
    假设一共有5页吧,我点4的时候,页面一直停在第一页,点其他的数字也是一样的。gridview分页事件也获取不了想要的页码...
    后来改成DropDownList实现了,但是这个问题还是很纠结。
    我失业了,因为我决定转行。外面大雪纷飞,我的种子在慢慢发芽。
    2010年4月5日 0:56

答案

  • 你好,

    应该不会有你说的这种情况。

    我自己自定义datatable作为gridview的数据源,并允许gridview分页,同时实现分页事件也是没有问题的。

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    System.Data.DataView CreateDataSource()
    {
    System.Data.DataTable dt = new System.Data.DataTable();
    System.Data.DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
    
    for (int i = 0; i < 8; i++)
    {
    System.Random rd = new System.Random(Environment.TickCount * i); ;
    dr = dt.NewRow();
    dr[0] = "班级" + i.ToString();
    dr[1] = "姓名"+ i.ToString();
    dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
    dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
    dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
    dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
    dt.Rows.Add(dr);
    }
        
    System.Data.DataView dv = new System.Data.DataView(dt);
    Session["test"] = dt;
    return dv;
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
        CreateDataSource();
        this.GridView1.DataSource = (Session["test"] as DataTable).DefaultView; ; 
    this.GridView1.DataBind();
    }
    }
    
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Header)
    {
    for (int i = 0; i < e.Row.Cells.Count; i++)
    {
    Response.Write(e.Row.Cells[i].Text);
    }
    }
    }
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        this.GridView1.DataSource = (Session["test"] as DataTable).DefaultView; ; 
        this.GridView1.DataBind();
    }
    
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        this.GridView1.DataSource = (Session["test"] as DataTable).DefaultView; ;
        this.GridView1.DataBind();
    }
    
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        this.GridView1.DataSource = (Session["test"] as DataTable).DefaultView; ; 
        this.GridView1.DataBind();
    }
    
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt=(Session["test"] as DataTable);
        DataView dv = dt.DefaultView;
        dv[e.RowIndex].Row.Delete();
        
        this.GridView1.DataSource = dv;
        this.GridView1.DataBind();
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataSource = (Session["test"] as DataTable).DefaultView;
        this.GridView1.DataBind();
    }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" 
        OnRowDataBound="GridView1_RowDataBound" AllowSorting="True" 
        AutoGenerateColumns="False" onrowediting="GridView1_RowEditing" 
        onrowupdating="GridView1_RowUpdating" 
        onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowdeleting="GridView1_RowDeleting" AllowPaging="True" PageSize="2" 
        onpageindexchanging="GridView1_PageIndexChanging">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField>
            <ItemTemplate>
            <%#Eval("学生班级") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox runat="server">
                </asp:TextBox></EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField> 
             <ItemTemplate>
              <%#Eval("学生姓名") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" onblur="if(this.value==''){this.focus();}"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                
                </EditItemTemplate>
                </asp:TemplateField>      
            <asp:CommandField ShowDeleteButton="True" />
         </Columns> 
    </asp:GridView>
    </form>
    </body>
    </html>
    

    Microsoft Online Community Support
    2010年4月6日 6:20