9

NameValue コレクションを GridView にバインドできるように変換するには、どのようなコレクションを使用する必要がありますか? 直接実行するとうまくいきませんでした。

aspx.cs のコード

  private void BindList(NameValueCollection nvpList)
  {
     resultGV.DataSource = list;
     resultGV.DataBind();
  }

aspx のコード

<asp:GridView ID="resultGV" runat="server" AutoGenerateColumns="False" Width="100%">
    <Columns>
         <asp:BoundField DataField="Key" HeaderText="Key" />
         <asp:BoundField DataField="Value" HeaderText="Value" />
    </Columns>
</asp:GridView>

どんなヒントでも大歓迎です。ありがとう。バツ。

4

6 に答える 6

8

NameValueCollection の代わりに Dictionary<string,string> を使用できますか。Dictionary<T,T> は IEnumerable を実装しているため、次のように LINQ を使用できます。

resultGV.DataSource = from item in nvpDictionary
                      select new { Key = item.Key, Value = item.Value };
resultGV.DataBind();

[編集] 実際には、辞書を次のように直接使用できる場合があります。

resultGV.DataSource = nvpDictionary;
resultGV.DataBind();

キー/値が希望どおりにマップされない場合は、いつでも LINQ に戻ることができます。LINQ では、フィールドの名前を好きな名前に変更することもできます。

[編集] Dictionary<T,T> を使用するように変更できない場合は、メソッドで NameValueCollection のコピーを Dictionary として作成し、それにバインドします。

private void BindList(NameValueCollection nvpList)
{
   Dictionary<string,string> temp = new Dictionary<string,string>();
   foreach (string key in nvpList)
   {
      temp.Add(key,nvpList[key]);
   }

   resultGV.DataSource = temp;
   resultGV.DataBind();
}

これを頻繁に行う場合は、ディクショナリに変換する拡張メソッドを記述して、そのように使用できます。

public static class NameValueCollectionExtensions
{
   public static Dictionary<string,string> ToDictionary( this NameValueCollection collection )
   {
      Dictionary<string,string> temp = new Dictionary<string,string>();
      foreach (string key in collection)
      {
          temp.Add(key,collection[key]);
      }
      return temp;
   }
}

private void BindList(NameValueCollection nvpList)
{
   resultGV.DataSource = nvpList.ToDictionary();
   resultGV.DataBind();
}
于 2008-10-20T02:21:17.553 に答える
6

列挙子はキーのみを返すため、少し注意が必要です。ただし、Container.DataItemを使用してKey値を取得してから、NameValueCollectionを調べて値を取得できます。

<asp:GridView id="gv" runat="server" AutoGenerateColumns="false">
   <Columns>
      <asp:TemplateField HeaderText="Key">
         <ItemTemplate><%# Container.DataItem %></ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Value">
         <ItemTemplate>
            <%# ((NameValueCollection)gv.DataSource)[(string)Container.DataItem] %>
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>
于 2008-10-21T13:05:06.740 に答える
2

最後に、拡張機能の実装で提案されたソリューションを使用しましたが、拡張機能自体は使用しませんでした。

  private void BindList(NvpList nvpList)
  {
     IDictionary dict = new Dictionary<string, string>();

     foreach (String s in nvpList.AllKeys)
        dict.Add(s, nvpList[s]);

     resultGV.DataSource = dict;
     resultGV.DataBind();
  }

たぶん、静的なヘルパー クラスを実行し、多くの場所ではなく 1 か所で翻訳を行ってくれます。この拡張機能は非常に便利です... :-)

ありがとう。バツ。

于 2008-10-21T12:41:36.600 に答える
1

データバインディングとキーと値へのアクセスにはStringDictionaryを使用するのが最善だと思います

Dim sDict as New StringDictionary
sDict.Add("1","data1")
sDict.Add("2","data2")
sDict.Add("3","data3")
...

CheckBoxList1.DataSource = sDict
CheckBoxList1.DataValueField = "key"
CheckBoxList1.DataTextField = "value"
CheckBoxList1.DataBind()
于 2011-08-13T10:56:22.763 に答える
1

入れ子になっている場合Repeater(またはGridView、私も確信しています)、Mark Brackett の回答を次のように変更する必要があります。そうしないと、次の名前のコントロールが見つからないという実行時エラーが発生します。rpt

<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
    <li>
        <%# Container.DataItem %>:
        <%# ((NameValueCollection)((Repeater)Container.Parent).DataSource)[(string)Container.DataItem] %>
    </li>    
</ItemTemplate>
</asp:Repeater>
于 2010-03-30T21:10:07.843 に答える
0

Dictionary (実際には SortedDictionary) を GridView にバインドし、列の名前を変更したいという同様の問題がありました。私にとってうまくいったのは、もう少し読みやすいものです。

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:TemplateField HeaderText="Attribute">
      <ItemTemplate>
        <%# ((KeyValuePair<string,string>)Container.DataItem).Key %>
      </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Value">
      <ItemTemplate>
        <%# ((KeyValuePair<string,string>)Container.DataItem).Value %>
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

これは、GridView が表示しようとしている現在の項目である Container.DataItem を取得し、それを実際のデータ型 (KeyValuePair) に強制してから、その項目の目的のプロパティを表示することによって機能します。

于 2012-03-29T17:19:39.870 に答える