string CatalogGuidSql = "select CatalogName,CatalogGuid from ProductCatalogs where [Language]='" + dr["Code"].ToString() + "'";
                DataTable dt2 = Mydata.GetTable(CatalogGuidSql);
                foreach (DataRow dr2 in dt2.Rows)
                {
                    CatalogGuids.Append("<option value=" + dr2["CatalogGuid"] + ">" + dr2["CatalogName"] + "</option>");
                }
TableCell cell = new TableCell();
DropDownList CatalogGuid = new DropDownList();
CatalogGuid.ID = "CatalogGuid";
CatalogGuid.CssClass = "input";
CatalogGuid.DataSource = CatalogGuids;     
  //这样绑定是错的,提示:数据源的类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。
CatalogGuid.DataBind();
cell.Controls.Add(CatalogGuid);请问应该如何绑定它的数据源啊?

解决方案 »

  1.   

    另外你那个foreach 也不需要的,要不然还要数据绑定干嘛
      

  2.   


    <asp:TemplateColumn HeaderText="...">
        <ItemTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" 
                DataSource='<%#Users/GetUsers()/GetUsers(Eval("ClassID").ToString()) %>' 
                DataTextField="UserName" DataValueField="UserID"></asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateColumn>
      

  3.   


    谢谢,但是dt2里面字段太多了啊,DropDownList只用到两个字段嘛
      

  4.   

    看错了,方法一
    string CatalogGuidSql = "select CatalogName,CatalogGuid from ProductCatalogs where [Language]='" + dr["Code"].ToString() + "'"; 
    DataTable dt2 = Mydata.GetTable(CatalogGuidSql); 
    DropDownList CatalogGuid = new DropDownList();
    TableCell cell = new TableCell(); 
    foreach (DataRow dr2 in dt2.Rows) 

        CatalogGuid.Items.Add(new ListItem(dr2["CatalogName"].ToString(),dr2["CatalogGuid"].ToString()))

    CatalogGuid.CssClass = "input"; 
    cell.Controls.Add(CatalogGuid); 
    方法二
    string CatalogGuidSql = "select CatalogName,CatalogGuid from ProductCatalogs where [Language]='" + dr["Code"].ToString() + "'"; DataTable dt2 = Mydata.GetTable(CatalogGuidSql); 
    DropDownList CatalogGuid = new DropDownList();
    TableCell cell = new TableCell(); CatalogGuid.DataTextFiled = "CatalogName";
    CatalogGuid.DataValueFiled = "Catalogid"
    CatalogGuid.CssClass = "input"; 
    CatalogGuid.DataSource = CatalogGuids;
    cell.Controls.Add(CatalogGuid);
      

  5.   

    方法二再加一句CatalogGuid.DataBind();
      

  6.   

    dt2里有再多字段都没关系,主要是设置DataTextFiled属性为你要显示的字段名,设置DataValueFiled属性为你要设为值的字段名就好了
      

  7.   

    谢谢两位,O15013245O有一个单词拼错了,DataTextFiled应该是DataTextField,呵呵