怎么在GridView里数据加载前先加载GridView模板列DropDownList里的数据?

解决方案 »

  1.   

    楼主的意图是什么?加载dropdownlist,然后更改选定项?可以先绑定gridview和dropdownlist,然后在gridview的RowDataBound事件中更改 下拉框选项。类似:
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void Databind()
        {
            GridView1.DataSource = bs.SelectAll();
            //指定主键
            GridView1.DataKeyNames = new string[] { "id" };
            GridView1.DataBind();        //绑定下拉框
            DropDownList1.DataSource = bsi.SelectAll();
            DropDownList1.DataTextField = "score";
            DropDownList1.DataValueField = "id";
            DropDownList1.DataBind();
        }
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("onmouseover", "bcolor=this.style.backgroundColor;this.style.backgroundColor='#f1f1ff'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=bcolor");        //如果为编辑情况下时,绑定下拉框控件。
            DropDownList ddlinfo = (DropDownList)e.Row.FindControl("ddlInfo");
            if (ddlinfo != null)
            {
                ddlinfo.DataSource = bsi.SelectAll();
                ddlinfo.DataTextField = "score";
                ddlinfo.DataValueField = "id";
                ddlinfo.DataBind();
                //选定为默认值
                ddlinfo.SelectedValue = ((Label)e.Row.FindControl("sp")).Text;
            }
        }
      

  2.   

    哈哈!非常感谢!把我从死胡同带出来了!
    将下面文本框和dropdownlist(数据源是ObjectDataSource从数据库取出的数据)里是数据保存到ViewState在上面的GridView中显示(没有连接数据库),dropdownlist保存在ViewState里的是索引
    在GridView模板列里dropdownlist要根据传过来的索引显示对应的数据。GridView模板列里dropdownlist的数据源是ObjectDataSource从数据库取
    出的数据!问题是GridView模板列里dropdownlist不能根据传过来的索引显示对应的数据。因为在GridView通过数据源ViewState加载后,才加载模板列dropdownlist
    里的数据!所以不能正常显示!
    求方法实现?