protected void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                //当鼠标停留时更改背景色
                //e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ccf'");
                //当鼠标移开时还原背景色
                //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((LinkButton)e.Row.Cells[9].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('是否要删除 " + e.Row.Cells[2].Text + " 的图片分类吗?')");            }
            else if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
            {
                DropDownList ddlpicClass = e.Row.Cells[2].FindControl("ddlPicClass") as DropDownList;
                if (ddlpicClass != null)
                {
                    ddlpicClass.DataTextField = this.GetPicName().Columns[1].ToString();//GetPicName()方法返回dataTable对象
                    ddlpicClass.DataValueField = this.GetPicName().Columns[0].ToString();
                    ddlpicClass.DataSource = this.GetPicName().DefaultView;
                    ddlpicClass.DataBind();
                }
            }
        }
    }
我想要做的是,在点击编辑按钮时,下拉框显示从数据库获取的一串数据,但显示出来的却不是默认选中了我们要的那一项。不知道各位有没有什么好的方法指导下小弟啊?

解决方案 »

  1.   

    要不要加上一个for循环来判断获取的数据呢
      

  2.   

    这个你可以通过获取它的Id来绑定啊,这个很简单的啊
    Convert.ToInt32(ddl.SelectedValue)
      

  3.   

     else if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
      {
      DropDownList ddlpicClass = e.Row.Cells[2].FindControl("ddlPicClass") as DropDownList;
      if (ddlpicClass != null)
      {
      ddlpicClass.DataTextField = this.GetPicName().Columns[1].ToString();//GetPicName()方法返回dataTable对象
      ddlpicClass.DataValueField = this.GetPicName().Columns[0].ToString();
      ddlpicClass.DataSource = this.GetPicName().DefaultView;
      ddlpicClass.DataBind();
    ddlpicClass.SelectValue="数据库中的值";  }
      }
      

  4.   

    前台<asp:TemplateField HeaderText="出版社">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlPublisher" runat="server" DataSourceID="obdsBindPublisher"
                            DataTextField="Name" DataValueField="Id" Width="143px">
                        </asp:DropDownList>
                        <asp:HiddenField ID="hdFlHiddenPublisher" runat="server" Value='<%# Eval("Publisher.Id") %>' />
                        <asp:ObjectDataSource ID="obdsBindPublisher" runat="server" SelectMethod="GetAllPublisher"
                            TypeName="Shop.BLL.PublisherManager"></asp:ObjectDataSource>
                    </EditItemTemplate>
                </asp:TemplateField>
    直接这样就可以了,绑定他的ID,用隐藏域!