环境VS2003
在datagrid1中有一模板列和一个编辑,更新,取消列,模板列的EditItemTemplate里面放置了一个dropdownlist1控件,这个dropdownlist1控件有3个item{高,中,低},当数据绑定到datagrid后,我点击行的编辑,模板列的dropdownlist1便出现了,可默认显示值总是'高',怎样可以让dropdownlist1在编辑时显示的值是我数据表的真实值

解决方案 »

  1.   

    trydropdownlist1.selectItem = e.items.cell["0"].Text.ToString();
      

  2.   

    找到你帮定datagrid1的数据源 然后跟dropdownlist1.SelectedValue 达成一致,
      

  3.   

    //请参考
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {  
    DropDownList dropTemp;
    string strState;
    if(e.Item.ItemType == ListItemType.EditItem)
    {
    strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString(); //对DropDownList做数据绑定
    dropTemp = (DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
    if(dropTemp != null)
    {
    SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
    string strSql = "select distinct state from authors";
    SqlCommand cmd = new SqlCommand(strSql, cn);
    cn.Open();
    dropTemp.DataSource = cmd.ExecuteReader();
    dropTemp.DataTextField = "state";
    dropTemp.DataBind();
    cn.Close(); //到DropDownList中根据type的值去找需要设置为选中状态的项目,将其设置为选中
    ListItem item = dropTemp.Items.FindByText(strState);
    if(item != null)
    {
    item.Selected = true;
    }
    }
    }
    }
      

  4.   

    http://shilei831115.blog.hexun.com/7783567_d.html看看吧
      

  5.   

    顶“高歌”, 顺便指正个小错误::ListItem item = dropTemp.Items.FindByText(strState);
    if(item != null)
    {
    item.Selected = true;
    }
    =========》》》》》改为ListItem item = (ListItem) dropTemp.Items.FindByText(strState);
    if(item != null)
    {
    item.Selected = true;
    }
      

  6.   

    mqc507() 这个例子不用强转的,你试试就知道