在DataGrid中使用模板列,在EditTemplate中使用DropDownList,可供用户选择一些选项,但是我想在开始编辑此列时,DropDownList选中和数据库中保存的值相对应的item,该如何做?

解决方案 »

  1.   

    可以在绑定时调用一个函数,在函数里面将ddl和SelectItemIndex设置一下
    <asp:TemplateColumn HeaderText="Read">
    <ItemTemplate>
    <asp:DropDownList ID="ddl" SelectedIndex ='<%# GetIndex(DataBinder.Eval(Container, "DataItem.colname")) %>' Runat ="server" ></asp:DropDownList>
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
      

  2.   

    看这个就解决了:
    http://www.ccw.com.cn/applic/prog/htm2003/20030828_13IYP.asp这个也可以,不过是英语的:
    http://www.csharphelp.com/archives/archive212.html
      

  3.   

    http://www.cnblogs.com/lovecherry/archive/2005/03/25/125525.html
      

  4.   

    <ItemTemplate>
    //...
    </ItemTemplate>
    <EditTemplate>
    <asp:DropDownList ID="ddl" Runat ="server" ></asp:DropDownList>
    </EditTemplate>在编辑事件里
    DropDownList ddl = e.Item.Cells[该模板列的Index].FindControl("ddl");
    ddl.DataSource = 要绑定的数据源;
    ddl.DataTextField="显示文本的字段";//如"EmployeeName"
    ddl.DataValueField="文本对应的值的字段";//如"EmployeeID"
    ddl.DataBind();ddl.SelectedValue = ((DataRowView)e.Item.DataItem).Row["文本对应的值的字段,如EmployeeID"].ToString();