1

我有 2 个字符串集合:

StringCollection ParameterIDs
StringCollection ParameterValues

是否能够将这两个 StringCollections 映射为 DataSource,例如:

repeater.DataSource = ParameterIDs (as row1) + ParameterValues (as row2);
repeater.DataBind();

并在转发器中使用它们,例如:

                <asp:Repeater ID="repeatParameters" runat="server">
                    <HeaderTemplate>
                        <table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td class="formLabel">
                                <asp:Label ID="lblParameterID" Text="<% #DataBinder.Eval(Container.DataItem,"ParameterIDs") %>" runat="server" MaxLength="50"></asp:Label><br />
                            </td>
                            <td class="formInputText">
                                <asp:Label ID="lblParameterValue" Text="<%#DataBinder.Eval(Container.DataItem,"ParameterValues") %>" runat="server" MaxLength="50"></asp:Label><br />
                            </td>
                        </tr>
                        <tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
4

5 回答 5

5

第一个想法:
将它们混搭在字典中并将其用作数据源。

第二个想法:使用您需要的值
创建一个DataSet 。

第三个想法:
使用KeyValuePair

正如您所看到的,有很多不同的方法可以做到这一点,但它们都有一个共同的元素:
创建一个存储和映射相应值的对象。

于 2009-06-30T16:17:38.110 回答
2

不,因为 DataSource 需要一个具有 ICollection 接口的对象。

就像其他人说的那样,您可以创建一个字典或类似的东西:

List<KeyValuePair<string,string>>

其中参数id为key,参数value为KeyValuePair中的值。

于 2009-06-30T16:20:05.880 回答
1

我会用您的字符串集合填充字典,然后绑定到字典。

于 2009-06-30T16:19:49.923 回答
1

我建议 2 个选项 1. 在 .NET 1.1 中,您没有通用列表,因此您可以在代码中手动加入集合,然后使用加入的集合作为数据源

        StringCollection joined = new StringCollection();

        foreach(string s in stringCollection1)
            joined.Add(s);
        foreach (string s in stringCollection2)
        {
            if (!joined.Contains(s))
                joined.Add(s);
        }
  1. 在 .NET 2.0 及更高版本中,我建议您使用 List 而不是 StringCollection 使用 AddRange() 您可以将另一个列表添加到现有列表中。

  2. 在 .NET 3.5 中,您可以使用 Linq 与 2 个列表相交

    public static IEnumerable<TSource> Intersect<TSource>(
    this IEnumerable<TSource> first,
    IEnumerable<TSource> second
    

    )

于 2009-06-30T16:34:53.717 回答
1

如果您使用的是 .NET 3.5,这将完全符合您的要求:

 StringCollection parameterIds = new StringCollection();
 StringCollection parameterValues = new StringCollection();
 parameterIds.Add("someID");
 parameterValues.Add("someValue");

 var dataSource = parameterIds.Cast<string>()
     .SelectMany(itemOne => parameterValues
     .Cast<string>(), (itemOne, item2) => new { Row1 = itemOne, Row2 = item2 });

 repeater.DataSource = dataSource;
 repeater.DataBind();

就像 Bogdan_Ch 所说,如果您使用的是 .NET 2.0 及更高版本,我建议您远离StringCollectionto 。List<string>这样做还有一个额外的好处,即您不需要使用Cast扩展方法。

于 2009-07-01T07:32:32.530 回答