【问题标题】:convert List<ListItem> to datatable in c#在 C# 中将 List<ListItem> 转换为数据表
【发布时间】:2013-12-03 12:26:06
【问题描述】:

在我的应用程序中,我需要从 2 个 SharePoint 列表(ListA 和 ListB)中获取项目作为 listitem-collection,然后如果 ListB 中的项目匹配,我需要显示 ListA 中的项目(ListA.empid == ListB.empid) .

var iPs = AListItem.AsEnumerable()
                   .Select(r => r.FieldValues["EmpID"])
                   .Union(BListItem.AsEnumerable()
                                   .Select(r => r.FieldValues["EmpID"]));

if (iPs.Count() > 0)
{
    List<ListItem> sample = (from row in AListItem.AsEnumerable()
                             join id in iPs
                             on row.FieldValues["EmpID"] equals id
                             select row).ToList();
}

但我需要数据表中的结果才能绑定到中继器控件。如何将List&lt;ListItem&gt; 转换为数据表?

【问题讨论】:

  • 不能直接用List绑定repeater吗?
  • 我不知道如何将 List 绑定到中继器控件。

标签: c# sharepoint


【解决方案1】:

正如我在评论中提到的,使用sample 作为Repeater 控件的数据源。

此 SO 链接提供了有关如何实现此目的的详细信息:

binding-a-generic-list-to-a-repeater-asp-net

概述是这样的:

// Here's your object that you'll create a list of
private class Products
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductPrice { get; set; }
}

// Here you pass in the List of Products
private void BindItemsInCart(List<Products> ListOfSelectedProducts)
{   
    // The the LIST as the DataSource
    this.rptItemsInCart.DataSource = ListOfSelectedProducts;

    // Then bind the repeater
    // The public properties become the columns of your repeater
    this.rptItemsInCart.DataBind();
}

ASPX 代码:

<asp:Repeater ID="rptItemsInCart" runat="server">
  <HeaderTemplate>
    <table>
      <thead>
        <tr>
            <th>Product Name</th>
            <th>Product Description</th>
            <th>Product Price</th>
        </tr>
      </thead>
      <tbody>
  </HeaderTemplate>
  <ItemTemplate>
    <tr>
      <td><%# Eval("ProductName") %></td>
      <td><%# Eval("ProductDescription")%></td>
      <td><%# Eval("ProductPrice")%></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </tbody>
    </table>
  </FooterTemplate>
</asp:Repeater>

【讨论】:

  • 好吧,我没有读到mysql标签,所以很快就摆脱了它,因为语法是sql server! :)
【解决方案2】:

我同意 Ric:也许绑定 List 更好。

但如果您确实需要转换为数据表,请查看以下链接:

How to convert a List into DataTable

http://www.c-sharpcorner.com/UploadFile/1a81c5/list-to-datatable-converter-using-C-Sharp/

【讨论】:

  • this.ipRepeater.DataSource = 样本; this.ipRepeater.DataBind();但它不工作
  • 有错误信息吗?另外,请参阅link
猜你喜欢
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多