使用datatable将sharepoint列表标题和内部名称绑定到dropdownlist

时间:2014-06-20 03:49:27

标签: sharepoint drop-down-menu datatable

我正在尝试从2个共享点列表中获取已归档的名称,并将它们放入webpart上的2个不同的下拉列表中。但是,下拉列表中有一些重复的字段名称。我想知道我的代码是否正确。还是有其他方法来实现这个目标吗?

DataTable table = new DataTable("table");

        DataColumn column;
        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "Title";
        table.Columns.Add(column);

        column = new DataColumn();
        column.DataType = Type.GetType("System.String");
        column.ColumnName = "Internal";
        table.Columns.Add(column);
 DataRow row;

        foreach (SPField f in importList.Fields)
        {
            row = table.NewRow();
            row["Title"] = f.Title;
            row["Internal"] = f.InternalName;
            table.Rows.Add(row);
        }

        ddlImport.DataSource = table;
        ddlImport.DataTextField = "Title";
        ddlImport.DataValueField = "Internal";
        ddlImport.DataBind();

1 个答案:

答案 0 :(得分:0)

您可以考虑删除隐藏字段

   foreach (SPField f in importList.Fields)
    {
     if (!f.Hidden)
      {
        row = table.NewRow();
        row["Title"] = f.Title;
        row["Internal"] = f.InternalName;
        table.Rows.Add(row);
     }
   }