我在gridview中添加了一个模板列,该模板列为一dropdownlist(用来显示性别),数据绑定时,第一页是正常的,当我点击下一页时,性别这一列的数据就不对了,请问这是为什么?

解决方案 »

  1.   

    因为下一页dropdownlist没有绑定
      

  2.   

    this.XXX.DataSource = this.XXX();
    this.XXX.DataBind();
      

  3.   

    我说的数据不对,是这样的,dropdownlist无论数据是男还是女,都显示为男,因为我编辑项时男是默认的,现在dropdownlist显示的数据和读出的数据不一致,即:有的是女但还是显示为男
      

  4.   

    重新绑定全男数据啊,根据dropdownlist的选择查找数据
      

  5.   

    你那个dropdownlist.selectValue要指定数据的哦.
      

  6.   

    把所有DataGrid中有关绑定的函数拎出来做成这个页面公用的
    在加载页面
    上翻页
    下翻页

    时候调用。
      

  7.   

    string connStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\NewWeb2011-4-11\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
            }
            catch
            {
                Response.Write("数据库打开失败");
                return;
            }
            string selectStr = "SELECT * FROM stu";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = selectStr;
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(ds);
                //Response.Write(ds.Tables[0].Rows[1].ToString());
                //Response.Write(ds.Tables.Count.ToString());
                DataTable dt = ds.Tables[0];
                this.GridView1.DataSource = dt;
                this.GridView1.DataBind();
                this.GridView1.DataKeyNames = new string[] { "sno"};
                DropDownList ddl = null;
                //if (!flag)
                //{
                //    ddl.Visible = false;
                //}
                //flag = true;
                Label label = null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddl = (DropDownList)this.GridView1.Rows[i].FindControl("DropDownList1");
                    ddl.Visible = false;
                    DataRowView drv = dt.DefaultView[i];
                    //
                    label = (Label)this.GridView1.Rows[i].FindControl("Label1");
                    if (Convert.ToString(drv["sex"]).Trim() == "男")
                    {
                        label.Text = "男";
                    }
                    else
                        label.Text = "女";
                 
                }
                conn.Close();
            }
            catch
            {
                Response.Write("数据读取失败");
                return;
            }
      

  8.   

     GridView 的DropDownList分页实现
      

  9.   

    程序也有异常:索引超出范围。必须为非负值并小于集合大小。参数名: index 
      

  10.   

    程序也有异常:索引超出范围。必须为非负值并小于集合大小。参数名: index 断点index的值 跟你的目标值比较。