datagrid中填加模板列,模板列中有一个label,当按下编辑后显示一个dropdownlist,该label已经绑定到数据库,请教如何能在按下编辑后dropdownlist随label的值变化,例如,label显示1,dropdownlist就显示“男”,label显示2,dropdownlist就显示女,dropdownlist中已经预先写好了两个固定的值男和女。现在不知道在哪里添加判断selectedindex的代码.

解决方案 »

  1.   

    在DataGrid的ItemDataBound事件中加
      

  2.   

    在e.Item.ItemType == ListItemType.EditItem的判断里
    用DropDownList myDropDownList = (DropDownList)e.Item.FindControl("你的DropDownList的名字");来获取DropDownList
    Convert.ToString(DataBinder.Eval(e.Item.DataItem,"字段名"))来获取该字段的值
    再用myDropDownList.Selectvalue来设置myDropDownList选择的值就可以了
    注意:
    myDropDownList的Item有datatext和datavalue的属性
    datatext就是显示出来的值,如"男"
    datavalue就是data的值,如1
    这样应该就OK了
    由于时间关系我也没有具体的试
    不过我做过一个类似的东东
    所以在思路上是还会有问题的
      

  3.   

    CSDN上以前见过一个这样的例子
      

  4.   

    to 珂楠:将什么绑定到Convert.ToString(DataBinder.Eval(e.Item.DataItem,"字段名")),我看到有人把selectindex绑定,可是类型不一样呀
      

  5.   

    有没有办法通过获得label的值来得到dropdownlist的selectindex,假设label的值只有男、女两种,通过indexof实现
      

  6.   

    DataBinder.Eval(e.Item.DataItem,"字段名")这个方法是通过你的DataGrid的DataSource获取你的当前行的对应字段的值
    Convert.ToString方法是将得到的值转成String型
    而Selectindex是让DropDownList通过索引号来选择Item
    而我告诉你的SelectValue则是通过值来选择Item
    在绑定数据时通过值来选择要好操作些,否则你还要通过值来判断索引再选择
    在设初始值的时候用electindex比较好,可以用DropDownList.Selectindexe = 0来让它选择第一项
      

  7.   

    Convert.ToString(DataBinder.Eval(e.Item.DataItem,"字段名"))
    所得到的就是你数据源中当前列"字段名"这个字段的值
    你用DropDownList myDropDownList = (DropDownList)e.Item.FindControl("你的DropDownList的名字");
    获取的DropDownList也就是myDropDownList.Selectedvalue = Convert.ToString(DataBinder.Eval(e.Item.DataItem,"字段名"))就可以了
    不过你必须保证你的DropDownList的Item里有Value为Convert.ToString(DataBinder.Eval(e.Item.DataItem,"字段名"))的Item
      

  8.   

    我在itemdatabound中这么写的,提示“未将对象引用设置到对象的实力”
    if(e.Item.ItemType==ListItemType.EditItem)
    {
    DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DDL_1");
    myDropDownList.SelectedValue = Convert.ToString(DataBinder.Eval(e.Item.DataItem,"yhxz"));
    }
      

  9.   

    我的思路是在按“编辑”前,将需要更改的这个单元格的数据记录下来,然后在按“编辑”后在dropdownlist中找到这个数据,并显示出来,可是不知道怎么记录,在哪儿写都提示“未将对象引用设置到对象的实力”,你说的绑定也一样
      

  10.   

    是在DropDownList myDropDownList = (DropDownList)e.Item.FindControl("DDL_1");
    这一行报错吗?
    你看看你的DropDownList是不是放在DataGrid模板列的EditItemTemplate那一栏
      

  11.   

    是的,在itemtemplate里放的是一个label。
      

  12.   

    报错是在myDropDownList.SelectedValue = Convert.ToString(DataBinder.Eval(e.Item.DataItem,"yhxz"));
      

  13.   

    而且确定yhxz的值在value中存在
      

  14.   

    你先跟进去看看,看你得到的DropDownList是否为null

    myDropDownList.SelectedValue = Convert.ToString(DataBinder.Eval(e.Item.DataItem,"yhxz"));
    处设段点
    看myDropDownList的值是否为null
    如果是的话就看看你的DropDownList的名字写的是否正确
    也就是e.Item.FindControl("DDL_1")中的"DDL_1"是否正确
      

  15.   

    在绑定数据的时候,,在ItemBound事件中,就要将数据榜定到DropDownList中,并进行选择合适的SelectItem.在Edit的时候,只是将ItemColumns的Visible为false,editItemColumns的visiable为true的操作而已
      

  16.   

    http://community.csdn.net/Expert/TopicView3.asp?id=4071048
    http://community.csdn.net/Expert/TopicView3.asp?id=4040085
    哪位热心人帮忙给顶一下这两个贴子
      

  17.   

    十分感谢conan1211(柯楠) 兄的热情帮助,现在问题终于解决了,的确是名字写错了,还有提醒(补天石),这里决不是visible为false那么回事。在编辑之前ddl_1根本就不存在。是这样吧?柯楠,分一会给,看看这个visible的问题还有什么说法。
      

  18.   

    yuelei6225(补天石) 说得没错
    因为
    当你点编辑的时候DataGrid的当前行还不是可编辑行
    在点了编辑之后再DataGrid.DataBind()之后
    当前行才能成为编辑行而FindControl方法是从前台找控件
    而Visble为false的话在前台,也就是HTML端是不生成代码的
    所以才会出现在Edit的时候找不到ItemColumns内的控件的情况(遇到过)
    不过这只是模板列的原理
      

  19.   

    nnxxyy1111(我不想做菜鸟) 说的 在编辑之前ddl_1根本就不存在
    也就是这个原因了