0

次の表を想像してください。

MatchID UserID  MatchField        MatchValue
7          5    MatchInterests        3
8          5    MatchInterests        7
9          5    MatchInterests       12
10         5    MatchInterests       20
11         5    MatchInterests       26
12         2    MatchInterests        7
13         2    MatchInterests       26
14         4    MatchInterests       26
15         5    MatchStates          12
16         5    MatchStates          11
17         2    MatchStates           1
18         2    MatchStates          17
19         4    MatchStates          10

私がやりたいことは、フィールド MatchInterests のユーザー 5 のリストボックスで MatchValues 値を事前に選択することです。したがって、事前に選択したいものの結果のデータセットは次のようになります。

MatchID UserID  MatchField        MatchValue
7          5    MatchInterests        3
8          5    MatchInterests        7
9          5    MatchInterests       12
10         5    MatchInterests       20
11         5    MatchInterests       26

これを行う最善の方法は何ですか?

私がやろうとしたことはこれです:

string strSQL2 = "SELECT MatchValue FROM tmpUsermatch WHERE MatchField = 'MatchInterests' AND UserID = '" + (DT2["UserID"].ToString()) + "'";
Interests.SelectionMode = ListSelectionMode.Multiple;
using (var con = new SqlConnection(strCon1))
using (var adapter2 = new SqlDataAdapter(strSQL2, con))
   {
     DataTable dt2 = new DataTable();
     adapter2.Fill(dt2);
     foreach (DataRow row in dt2.Rows)
     {
       Interests.SelectedValue = row["MatchValue"].ToString();
     }
   }

これは機能しますが、データセットの最後のレコードの値が選択されるだけで、すべてのレコードの値を選択する必要があります。

As Requested : コントロール名 Interests のマークアップ。

<tr>
   <td style="width:160px">
      <asp:Label ID="Label26" runat="server" AssociatedControlID="Interests">Interests:</asp:Label>
   </td>
   <td style="width:300px">
      <div id="UCStyle1">
        <asp:ListBox ID="Interests" SelectionMode="Multiple" runat="server">
        </asp:ListBox>
      </div>
   </td>
</tr>
4

1 に答える 1

1

ループの反復ごとに効果的に上書きSelectedValueしているため、最後に選択されたものだけが表示されます。

Selected項目自体にプロパティを設定するか、ListBox.SetSelected()メソッドを使用する必要があります。メソッドのドキュメント ページに例があります。

だから、代わりに

Interests.SelectedValue = row["MatchValue"].ToString();

あなたが持っているだろう

Interests.SetSelected(x, true);

x、選択するリスト項目のインデックスです。row["MatchValue"]インデックスが手元にない場合は、アイテムを取得するなどして、インデックスを作成する必要がある場合があります。

于 2015-10-30T12:27:45.197 に答える