.net中 现在有一个gridview  首次进入这个页面的时候 显示前100条 显示正常  分页也正常      后来根据条件查询  我调试了 数据是有的  但是进不了RowDataBound事件  所以页面就显示不出来数据  这是为什么?????????????????????????????????????

解决方案 »

  1.   

     初始页面
     this.GvUsePotion.DataSource = PotionInfoManager.GetAllPotionName();
            if (PotionInfoManager.GetAllPotionName().Count > 0)
            {
                this.lblCount.Text = "共查询到" + PotionInfoManager.GetAllPotionName().Count + "条数据";
                this.lblCount.Style.Add("color", "black");
            }
            else 
            {
                this.lblCount.Text = "共查询到0条数据";
                this.lblCount.Style.Add("color","red");
            }根据条件查询
     protected void imgCount_Click(object sender, EventArgs e)
        {
           
    string startTime = this.txtStartTime.Text.Trim();
            string endTime = this.txtEndTime.Text.Trim();
            string num1 = this.txtnum1.Text.Trim();
            string num2 = this.txtnum2.Text.Trim();
            string userid = this.ddlUserId.SelectedItem.Text.Trim();
            string name =this.ddlPotionInfo.SelectedItem.Text.Trim();
            string price1 = this.txtPrice1.Text;
            string price2 = this.txtPrice2.Text;
            IList<PotionInfo> list = PotionInfoManager.GetCountPotionInfoByOrder(startTime, endTime, num1, num2, price1, price2, name, userid);
            if (list.Count > 0)
            {            this.lblMess.Text = "";
                
                this.lblCount.Text = "共查询到" + list.Count + "条记录";
                this.lblCount.Style.Add("color", "black");
            }
            else
            {
                this.lblMess.Text = "没有符合条件的药剂可统计!";
                this.lblCount.Text = "共查询到0条记录";
                this.lblCount.Style.Add("color","red");
            }
            this.GvUsePotion.DataBind();}protected void GvUsePotion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                int id = Convert.ToInt32(this.GvUsePotion.DataKeys[e.Row.RowIndex]["PotionId"].ToString());
                PotionInfo p = PotionInfoManager.GetPotionInfoById(id);
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#95CBE5'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor"); 
                sunCount = PotionInfoManager.GetSunCount(p.PotionName);
                useCount = PotionInfoManager.GetUseNum(p.PotionName);
                (e.Row.FindControl("lbl2") as Label).Text = sunCount.ToString();
                (e.Row.FindControl("lbl3") as Label).Text = useCount.ToString();
                (e.Row.FindControl("lbl4") as Label).Text = (sunCount - useCount).ToString();
                (e.Row.FindControl("lbl6") as Label).Text = Convert.ToString(sunCount*Convert.ToDouble(p.PotionPrice));
                (e.Row.FindControl("lbl7") as Label).Text =PotionInfoManager.getPotionAddNum(p.PotionName).ToString();
                if (sunCount == useCount)
                {
                    (e.Row.FindControl("lbl8") as Label).Text = "已用完";
                    (e.Row.FindControl("lbl8") as Label).Style.Add("color","red");
                }
                else 
                {
                    (e.Row.FindControl("lbl8") as Label).Text = "未用完";
                    (e.Row.FindControl("lbl8") as Label).Style.Add("color", "blue");
                }
             
              
            }
        }
      

  2.   

    噢噢  没人明白吗 就是有一个gridview 首次进入这个页面的时候 显示前100条 显示正常 分页也正常 后来根据条件查询 我调试了 数据是有的 但是进不了RowDataBound事件 所以页面就显示不出来数据  最重要的是查询时为什么进不了RowDataBound事件   
      

  3.   

    相信应该是查询click,致使页面重新加载的问题,所以需要看下page_load时都执行了些什么~
      

  4.   

    自定义GRIDVIEW,转换思路,用另一种方法,分页请自己定义<asp:button>,设置后台事件!!!
    public partial class _Default : System.Web.UI.Page
    {//清清月儿http://blog.csdn.net/21aspnet  
      SqlConnection sqlcon;
      SqlCommand sqlcom;
      string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码";
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      bind();
      }
      }
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
      {
      GridView1.EditIndex = e.NewEditIndex;
      bind();
      }//删除
      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
      string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
      sqlcon = new SqlConnection(strCon);
      sqlcom = new SqlCommand(sqlstr,sqlcon);
      sqlcon.Open();
      sqlcom.ExecuteNonQuery();
      sqlcon.Close();
      bind();
      }//更新
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
      sqlcon = new SqlConnection(strCon);
      string sqlstr = "update 表 set 字段1='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
      + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
      + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
      sqlcom=new SqlCommand(sqlstr,sqlcon);
      sqlcon.Open();
      sqlcom.ExecuteNonQuery();
      sqlcon.Close();
      GridView1.EditIndex = -1;
      bind();
      }//取消
      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
      GridView1.EditIndex = -1;
      bind();
      }//绑定
      public void bind()
      {
      string sqlstr = "select * from 表";
      sqlcon = new SqlConnection(strCon);
      SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
      DataSet myds = new DataSet();
      sqlcon.Open();
      myda.Fill(myds, "表");
      GridView1.DataSource = myds;
      GridView1.DataKeyNames = new string[] { "id" };//主键
      GridView1.DataBind();
      sqlcon.Close();
      }
    }前台主要代码:
      ... ...
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
      ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
      OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
      <Columns>
      <asp:BoundField DataField="身份证号码" HeaderText="用户ID" ReadOnly="True" />
      <asp:BoundField DataField="姓名" HeaderText="用户姓名" />
      <asp:BoundField DataField="员工性别" HeaderText="性别" />
      <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" />
      <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
      <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
      <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
      </Columns>
      <RowStyle ForeColor="#000066" />
      <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
      <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
      <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
      </asp:GridView>
    3.GridView正反双向排序:
    效果图:点姓名各2次的排序,点其他也一样可以。后台代码:
    using System;
    using System.Data;
    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;
    using System.Data.SqlClient;
    public partial class Default3 : System.Web.UI.Page
    {//清清月儿的博客http://blog.csdn.net/21aspnet  
      SqlConnection sqlcon;
      string strCon = "Data Source=(local);Database=北风贸易;Uid=sa;Pwd=";
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      ViewState["SortOrder"] = "身份证号码";
      ViewState["OrderDire"] = "ASC";
      bind();
      }
      }
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
      string sPage = e.SortExpression;
      if (ViewState["SortOrder"].ToString() == sPage)
      {
      if (ViewState["OrderDire"].ToString() == "Desc")
      ViewState["OrderDire"] = "ASC";
      else
      ViewState["OrderDire"] = "Desc";
      }
      else
      {
      ViewState["SortOrder"] = e.SortExpression;
      }
      bind();
      }  public void bind()
      {
        
      string sqlstr = "select top 5 * from 飞狐工作室";
      sqlcon = new SqlConnection(strCon);
      SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
      DataSet myds = new DataSet();
      sqlcon.Open();
      myda.Fill(myds, "飞狐工作室");
      DataView view = myds.Tables["飞狐工作室"].DefaultView;
      string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
      view.Sort = sort;
      GridView1.DataSource = view;
      GridView1.DataBind();
      sqlcon.Close();
      }
    }前台主要代码:
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
      CellPadding="3" Font-Size="9pt" OnSorting="GridView1_Sorting" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
      <FooterStyle BackColor="White" ForeColor="#000066" />
      <Columns>
      <asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" />
      <asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/>
      <asp:BoundField DataField="员工性别" HeaderText="性别" SortExpression="员工性别"/>
      <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>
        
      </Columns>
      <RowStyle ForeColor="#000066" />
      <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
      <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
      <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
      </asp:GridView>
      

  5.   

    一语惊醒梦中人啊 看了 page_load中  执行的是初始页面方法
      

  6.   

    还是不对啊  现在在page_load换成了查询时绑定的方法  还是不行 绑不了数据
      

  7.   

    1. 贴出PageLoad代码~
    2. 断点跟入useridimgCount_Click,看看startTime, endTime, num1, num2, price1, price2, name这些参数都是什么值,返回数据集有没有数据~
      

  8.   

    this.ddlUserId.DataSourceID = null;
            this.ddlUserId.DataSource = UserInfoManager.GetAllUser();
            this.ddlUserId.DataBind();
            this.ddlPotionInfo.DataSourceID = null;
            this.ddlPotionInfo.DataSource = PotionInfoManager.GetAllPotionName();
            this.ddlPotionInfo.DataBind();
            this.GvUsePotion.DataSourceID = null;
            this.GvUsePotion.DataSource = PotionInfoManager.GetAllPotionName();
            if (PotionInfoManager.GetAllPotionName().Count > 0)
            {
                this.lblCount.Text = "共查询到" + PotionInfoManager.GetAllPotionName().Count + "条数据";
                this.lblCount.Style.Add("color", "black");
            }
            else 
            {
                this.lblCount.Text = "共查询到0条数据";
                this.lblCount.Style.Add("color","red");
            }
            this.GvUsePotion.DataBind();
    有值啊 就是显示不出来  我说useridimgCount_Click里面有值  就是在页面上显示不出来