在DataGrid中绑定的DropDownList数据列表,怎么老是重复增加数据?
我分析是在DataGrid中ItemDataBound时列表已绑定一次,在编辑时点击DropDownList列表又绑定了一次,但如何只显示填充一个列表呢?请大写帮忙解决一下!页面文件有DataGrid,采用模板列如下:
<asp:TemplateColumn HeaderText="部门">
<ItemTemplate>
<asp:Label runat="server" ID=DeptName Text='<%# DataBinder.Eval  (Container, "DataItem.Department") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate> <asp:DropDownList runat="server" ID="ddlDepartment"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>在CS文件中代码如下:
private void drgPerson_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataView dvDept=GetDataSet().Tables["department"].DefaultView; if(e.Item.ItemType==ListItemType.EditItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
string current = drv["Department"].ToString(); DropDownList ddlCurrentDept = (DropDownList) e.Item.FindControl("ddlDepartment");

ddlCurrentDept.DataSource=dvDept;
ddlCurrentDept.DataTextField="Department";
ddlCurrentDept.DataValueField="DeptID";
ddlCurrentDept.DataBind(); ddlCurrentDept.SelectedIndex = ddlCurrentDept.Items.IndexOf(ddlCurrentDept.Items.FindByText(current));
 DataTextField="Department" 
                                      DataValueField="DeptID"
}
}

解决方案 »

  1.   

    我在这里加上这个语名就没有数据显示了,还是不行呀!
    if (!this.Page.IsPostBack)
    {
    ddlCurrentDept.DataSource=dvDept;
    ddlCurrentDept.DataTextField="Department";
    ddlCurrentDept.DataValueField="DeptID";
    ddlCurrentDept.DataBind();
    }
      

  2.   

    我插一嘴
      
      是不是 DropDownList 在页面显示的时候是  最高层~~~~  容器的层可以高于DropDownList  但是随出来的问题是 容器里面的 东西怎么和完结联系???
      

  3.   

    DropDownList是嵌套在DataGrid中的模板列,显示的是数据库中的数据,如何让它只显示一次列表,而不是每点击一次就重复一次数据,还是没有解决!
      

  4.   

    实在不行就把DropDownList先清空,再绑定
      

  5.   

    绑定之前先清空
    private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    string strSql;
    DropDownList DropDownList1 = new DropDownList();
    try
    {

    if (!myclsDbCommon.OpenConnection())
    {
    System.Diagnostics.Debug.Write("不能连接数据库!");
    }  if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
    DropDownList1 = (DropDownList)e.Item.Controls[2].FindControl("DropDownList1");
    strSql = "SELECT DISTINCT";
    strSql += "   ID ";
    strSql += "  FROM Employee";    OleDbDataReader  myReader = null;
    if (!myclsDbCommon.QueryWithDataReader(strSql,ref myReader))
    {
    System.Diagnostics.Debug.Write("SQL错误!");
    } DropDownList1.Items.Clear();                    
    DropDownList1.Items.Add("");
    while(myReader.Read())
    {
    DropDownList1.Items.Add(myReader["ID"].ToString());
    }
    myReader.Close();
    myReader = null;
    }     }
    catch(Exception ex)
    {
    System.Diagnostics.Debug.Write(ex.ToString());
    }
    finally
    {

    if (!myclsDbCommon.CloseConnection())
    {
    System.Diagnostics.Debug.Write("数据库关闭所错误!");
    }
    }
    }