我自己设置的datasource..运行之后在GridView中不分页..但是在VS2005编辑界面里可以看到分页,..请大家帮忙看一下错在哪?该怎么改?代码如下:using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class baojiamain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Text = "2007-1-1";
        //TextBox1.Text = DateTime.Now.Year.ToString() + ("-") + DateTime.Now.Month.ToString() + ("-") + "1";
        TextBox2.Text = "2099-12-31";
        if (!IsPostBack)
        {
            GridView1.DataSource = mybind();
            GridView1.DataBind();
            GridView2.DataSource = mybind2();
            GridView2.DataBind();
        }
    }
   
    public DataView mybind()
    {        SqlConnection lj = new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
        lj.Open();
        string sqla = "select * from sa_quomain order by ccode desc";
        SqlDataAdapter da = new SqlDataAdapter(sqla, lj);
        DataSet ds = new DataSet();
        da.Fill(ds, "congbiao");
        return ds.Tables["congbiao"].DefaultView;
     
    }    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
      
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
                }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("cd.aspx");
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;
    }    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#33cc00'");            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DEDFDE'");
        }
    }
    protected void Button8_Click(object sender, EventArgs e)
    {        SqlConnection lj = new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
        lj.Open();
        string sql = "select * from sa_quomain";
        if (TextBox6.Text.Trim().Length != 0)
        {
            sql += " where ccode='" + TextBox6.Text.Trim() + "'";
        }
        else
            sql += " where ccode like '%" + "" + "%'";
        if (TextBox7.Text.Trim().Length != 0)
        {
            sql += " and ccuscode='" + TextBox7.Text.Trim() + "'";
        }
        if (TextBox3.Text.Trim().Length != 0)
        {
            sql += " and cpersoncode='" + TextBox3.Text.Trim() + "'";
        }
        if (TextBox4.Text.Trim().Length != 0)
        {
            sql += " and cmaker='" + TextBox4.Text.Trim() + "'";
        }
        if (TextBox1.Text.Trim().Length != 0)
        {
            sql += " and ddate>='" + TextBox1.Text.Trim() + "'";
        }
        if (TextBox2.Text.Trim().Length != 0)
        {
            sql += " and ddate<='" + TextBox2.Text.Trim() + "'";
        }
        sql += " order by ccode desc";
        SqlDataAdapter da = new SqlDataAdapter(sql, lj);
        DataSet ds = new DataSet();
        da.Fill(ds, "zb");
        if (ds.Tables["zb"].Rows.Count != 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            Response.Write("<script>alert('没有你要查找的记录!')</script>");        }
        lj.Close();
    }    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("zjbj1.aspx");
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("zjbj2.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("output.aspx");
    }    public void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string mxid = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
        Session["mxid"] = mxid;
        SqlConnection lj = new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
        lj.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from sa_quodetails where id='" + mxid + "' order by autoid desc ", lj);
        DataSet ds = new DataSet();
        da.Fill(ds, "sa_quodetails");
        GridView2.DataSource = ds;
        GridView2.DataBind();
        lj.Close();    }
    public DataView mybind2()
    {        SqlConnection lj = new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
        lj.Open();
        string sqlabc = "select * from sa_quodetails where id='" + Session["mxid"] + "'";
        SqlDataAdapter da = new SqlDataAdapter(sqlabc, lj);
        DataSet ds = new DataSet();
        da.Fill(ds, "sa_quodetails");
        return ds.Tables["sa_quodetails"].DefaultView;
        lj.Close();    }    
    
    protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;
    }    protected void Button4_Click(object sender, EventArgs e)
    {
        SqlConnection abb = new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
        abb.Open();
        string sqlb = "select * from sa_quodetails where id='" + Session["mxid"] + "'";
        
        string sqld = "delete from sa_quomain where id='" + Session["mxid"] + "'";
        SqlCommand cmd1 = new SqlCommand(sqlb, abb);
        SqlDataReader dr = cmd1.ExecuteReader();
        if (dr.Read())
        {
            Response.Write("<script>alert('因明细存在,系统不允许删除此报价单!')</script>");        }
        else
        {
            dr.Close();
            SqlCommand cmd = new SqlCommand(sqld, abb);
            cmd.ExecuteNonQuery();
            abb.Close();
            GridView1.DataSource = mybind();
            GridView1.DataBind();
        }    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {        string wyid = GridView2.Rows[e.RowIndex].Cells[0].Text.ToString();
        SqlConnection lj= new SqlConnection(ConfigurationSettings.AppSettings["myconn"]);
        lj.Open();
        string sqlmm = "delete from sa_quodetails where autoid='" + wyid + "'";
        SqlCommand cmdmm = new SqlCommand(sqlmm, lj);
        cmdmm.ExecuteNonQuery();
        lj.Close();
        GridView2.DataSource = mybind2(); ;
        GridView2.DataBind();
           }    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#33cc00'");            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DEDFDE'");
        }    }
}

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="baojiamain.aspx.cs" Inherits="baojiamain" Debug="true" %>
    <asp:Button ID="Button8" runat="server" Height="20px" Text=" 确定 " OnClick="Button8_Click" Width="50px" />&nbsp;
    <asp:Button ID="Button3" runat="server" Height="20px" Text="增加报价单" OnClick="Button3_Click" />&nbsp;
            <asp:Button ID="Button5" runat="server" Height="20px" Text="增加明细" OnClick="Button5_Click" />&nbsp; <asp:Button ID="Button4" runat="server" Height="20px" Text="删除报价单" OnClientClick="return confirm('请确定是否要删除此报价单?');" OnClick="Button4_Click"/>&nbsp;
            &nbsp; &nbsp;<asp:Button ID="Button1" runat="server" Height="20px" Text="打印" OnClick="Button1_Click" />&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button2" runat="server" Height="20px" OnClick="Button2_Click" Text="返回" />
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
                CellSpacing="1" DataKeyNames="ID" Font-Size="9pt"
                GridLines="None" PageSize="18" Width="669px" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanging="GridView1_SelectedIndexChanging">
                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="ID" />
                    <asp:BoundField DataField="cSTCode" HeaderText="销售类型">
                        <ItemStyle HorizontalAlign="Center" Width="60px" />
                        <FooterStyle Font-Size="9pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="dDate" DataFormatString="{0:d}" HeaderText="日期" HtmlEncode="False">
                        <ItemStyle Width="70px" HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:HyperLinkField DataNavigateUrlFields="ccode" DataNavigateUrlFormatString="bj1.aspx?ccode={0}"
                        DataTextField="ccode" HeaderText="报价单编号">
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:HyperLinkField>
                    <asp:BoundField DataField="cCusCode" HeaderText="客户编号" SortExpression="cCusCode" >
                        <ItemStyle HorizontalAlign="Center" Width="65px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="cexch_name" HeaderText="币种" SortExpression="cexch_name" >
                        <ItemStyle HorizontalAlign="Center" Width="50px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="iExchRate" HeaderText="汇率" SortExpression="iExchRate" >
                        <ItemStyle HorizontalAlign="Center" Width="40px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="cPersonCode" HeaderText=" 业务员" >
                        <ItemStyle HorizontalAlign="Center" Width="55px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="cMaker" HeaderText="制单" SortExpression="cMaker" >
                        <ItemStyle HorizontalAlign="Center" Width="35px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="cVerifier" HeaderText="审核" SortExpression="cVerifier" >
                        <ItemStyle HorizontalAlign="Center" Width="35px" />
                    </asp:BoundField>
                    <asp:CommandField HeaderText="选中" ShowSelectButton="True" >
                        <ItemStyle HorizontalAlign="Center" />
                        
                    </asp:CommandField>
                </Columns>
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#33CC00" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="SlateGray" Font-Bold="True" ForeColor="#E7E7FF" />
               <PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
               <PagerStyle HorizontalAlign="Center" BackColor="#C6C3C6" ForeColor="Black" />
            </asp:GridView>
            <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
                CellSpacing="1" GridLines="None" Width="669px" OnRowDeleting="GridView2_RowDeleting" OnRowCreated="GridView2_RowCreated" OnRowDataBound="GridView2_RowDataBound">
                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                <Columns>
                    <asp:BoundField DataField="autoid" HeaderText="ID">
                        <ItemStyle Font-Size="9pt" Width="40px" />
                        <HeaderStyle Font-Size="9pt" />
                    </asp:BoundField>
                    <asp:HyperLinkField DataTextField="AutoID" HeaderText="明细编号" Text="autoid" DataNavigateUrlFields="AutoID" DataNavigateUrlFormatString="bj2.aspx?AutoID={0}">
                        <ItemStyle Width="55px" Font-Size="9pt" />
                        <HeaderStyle Font-Size="9pt" />
                    </asp:HyperLinkField>
                    <asp:BoundField DataField="cInvCode" HeaderText="存货编码" SortExpression="cInvCode">
                        <ItemStyle Font-Size="9pt" Width="80px" />
                        <HeaderStyle Font-Size="9pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="iQuantity" HeaderText="数量" SortExpression="iQuantity">
                        <ItemStyle Font-Size="9pt" HorizontalAlign="Center" Width="40px" />
                        <HeaderStyle Font-Size="9pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="iQuotedPrice" HeaderText="报价" SortExpression="iQuotedPrice">
                        <ItemStyle Font-Size="9pt" HorizontalAlign="Center" Width="40px" />
                        <HeaderStyle Font-Size="9pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="cCusInvName" HeaderText="规格型号" SortExpression="cCusInvName">
                        <ItemStyle Font-Size="9pt" />
                        <HeaderStyle Font-Size="9pt" />
                    </asp:BoundField>
                    <asp:CommandField HeaderText="操作" ShowDeleteButton="True">
                        <HeaderStyle Font-Size="9pt" />
                        <ItemStyle Font-Size="9pt" Width="30px" />
                    </asp:CommandField>
                </Columns>
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Center" />
                <HeaderStyle BackColor="SlateGray" Font-Bold="True" ForeColor="#E7E7FF" />
            </asp:GridView>
            &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
            <br />
            <br />    </div>
        </form>
        
    </body></html>
      

  2.   

    关键得把这个问题解决先呀..卡在这里进退两难..初学.net,这个问题我实在是搞不定了...诚心请教各位高手..
      

  3.   

    <asp:GridView   ID="GridView2"   runat="server"   AllowPaging="True"   AutoGenerateColumns="False" 
    允许分页我已经设置为"True"了..我在VS2005里面的编辑界面可以看到分页的页码..但是一运行就没了..
      

  4.   

    晕```可能是你的设置每页的数据条数大了```
    你一共才10条记录``你的PageSize=18
    运行后怎么会分页呢```
      

  5.   

    如果你在vs里面都看到有分页,可能是因为你的行数不够没有分页!你仔细看看.你查询出来多少条记录!还有你的gridview的foot模板显示出来了吗?
      

  6.   

    我查询出来的有9百多条...foot模版显示不出来...
      

  7.   

    分頁後需要重新綁定datasource  
    protected   void   GridView1_PageIndexChanging(object   sender,   GridViewPageEventArgs   e) 

     GridView1.DataSource = l_dt;//必須再次把datasource拿出來綁上,因為分頁後display的數據,實際上只是看到的那一頁,並不是所有的page都有數據
     GridView1.PageIndex   =   e.NewPageIndex; 
     GridView1.DataBind();

      

  8.   

    在页面加载时已经有一次分页(我的程序显示不了分页),然后在点击查询时,如果查询记录数大于pagesize,也应该会分页(我的程序还是显示不了),Gridview中始终只显示一页....试过楼上所有做法.均行不通,不知道问题出在哪里..
      

  9.   

    把你的HTML的代码贴出来,不一定出在数据上
      

  10.   

    删除你的Gridview模板中的空格看看
      

  11.   

    LZ,你不要把DATAVIEW绑定,直接绑定DATATABLE,看看,我觉得是dataview的问题
      

  12.   

    还是不行..我想过了,问题应该出在绑定上..请教各位:Gridview1加载时的sql数据源和我查询后的绑定的sql数据源不一样,而pageindexchanging 只有一个,我如何在pageindexchanging事件中对两次绑定都进行正确的操作,因为按我的代码就是:即使我能分页..那么当我查询之后点击下一页的时候它又自动回到第一个数据源绑定的分页中去了.