• <i id='0O1wg'><tr id='0O1wg'><dt id='0O1wg'><q id='0O1wg'><span id='0O1wg'><b id='0O1wg'><form id='0O1wg'><ins id='0O1wg'></ins><ul id='0O1wg'></ul><sub id='0O1wg'></sub></form><legend id='0O1wg'></legend><bdo id='0O1wg'><pre id='0O1wg'><center id='0O1wg'></center></pre></bdo></b><th id='0O1wg'></th></span></q></dt></tr></i><div id='0O1wg'><tfoot id='0O1wg'></tfoot><dl id='0O1wg'><fieldset id='0O1wg'></fieldset></dl></div>
  • <tfoot id='0O1wg'></tfoot>

    <legend id='0O1wg'><style id='0O1wg'><dir id='0O1wg'><q id='0O1wg'></q></dir></style></legend>

    <small id='0O1wg'></small><noframes id='0O1wg'>

    • <bdo id='0O1wg'></bdo><ul id='0O1wg'></ul>

        .NET 3.5 列表框选定值(Winforms)

        时间:2023-10-08

          <bdo id='IPg2V'></bdo><ul id='IPg2V'></ul>
        • <tfoot id='IPg2V'></tfoot>
            <tbody id='IPg2V'></tbody>

            <small id='IPg2V'></small><noframes id='IPg2V'>

            • <i id='IPg2V'><tr id='IPg2V'><dt id='IPg2V'><q id='IPg2V'><span id='IPg2V'><b id='IPg2V'><form id='IPg2V'><ins id='IPg2V'></ins><ul id='IPg2V'></ul><sub id='IPg2V'></sub></form><legend id='IPg2V'></legend><bdo id='IPg2V'><pre id='IPg2V'><center id='IPg2V'></center></pre></bdo></b><th id='IPg2V'></th></span></q></dt></tr></i><div id='IPg2V'><tfoot id='IPg2V'></tfoot><dl id='IPg2V'><fieldset id='IPg2V'></fieldset></dl></div>
            • <legend id='IPg2V'><style id='IPg2V'><dir id='IPg2V'><q id='IPg2V'></q></dir></style></legend>

                  本文介绍了.NET 3.5 列表框选定值(Winforms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在努力从启用了多选并已绑定到获取名称(作为 DisplayMember)和 ID(作为 ValueMember)的数据库表的 Winforms 列表框中获取选定的值(请注意 VALUES 不是 TEXT)-我需要所选项目的 ID.

                  I am BATTLING to get the selected values (please note VALUES not TEXT) from a Winforms Listbox that has multi-select enabled and has been bound to a database table getting the Name (as DisplayMember) and ID (as ValueMember) - I need the ID of the selected items.

                  列表框控件具有 SelectedValue 的属性,用于获取选定项值之一,但不是所有选定项值.

                  The listbox control has properties for SelectedValue to get one of the selected items values, but not for all selected items values.

                  SelectedItems 属性返回一个 Listbox.SelectedObjectCollection,我似乎无法从中提取项目的值.

                  The SelectedItems property returns a Listbox.SelectedObjectCollection from which I cannot seem to extract the VALUES of the items.

                  请帮忙!谢谢.

                  推荐答案

                  尝试将集合中的每个 object 转换为所需的 type.例如,如果我的商品是 Customer 类型,我可以这样做...

                  Try casting each object in the collection to the desired type. For example, if my items are of type Customer, I could do something like this...

                  var selected = listBox1.SelectedItems;
                  
                  foreach ( var item in selected )
                  {
                      var singleCustomer = (Customer)item;
                  }
                  

                  现在你可以从客户那里得到你想要的任何属性.

                  Now you can get any property you want from the Customer.

                  这只是一个简单的例子,但我相信你可以将这个概念应用到你的问题中.

                  This is just a trivial example, but I'm sure you can apply the concept to your problem.

                  更新(在更新问题以指示列表框绑定到表之后):

                  如果您绑定到 DataTable,您可以尝试这样的事情(同样,微不足道但相关):

                  If you're bound to a DataTable, you could try something like this (again, trivial but relevent):

                  var selected = listBox1.SelectedItems;
                  
                  foreach ( var item in selected )
                  {
                      var itemArray = ( (DataRowView)item ).Row.ItemArray;
                  
                      var name = itemArray[0];
                      var id = itemArray[1];
                  }
                  

                  这篇关于.NET 3.5 列表框选定值(Winforms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:更改选定颜色列表框 下一篇:列表框项目WPF,不同项目的不同背景颜色

                  相关文章

                  • <bdo id='zCkyg'></bdo><ul id='zCkyg'></ul>

                1. <tfoot id='zCkyg'></tfoot>
                2. <legend id='zCkyg'><style id='zCkyg'><dir id='zCkyg'><q id='zCkyg'></q></dir></style></legend>

                  <small id='zCkyg'></small><noframes id='zCkyg'>

                  <i id='zCkyg'><tr id='zCkyg'><dt id='zCkyg'><q id='zCkyg'><span id='zCkyg'><b id='zCkyg'><form id='zCkyg'><ins id='zCkyg'></ins><ul id='zCkyg'></ul><sub id='zCkyg'></sub></form><legend id='zCkyg'></legend><bdo id='zCkyg'><pre id='zCkyg'><center id='zCkyg'></center></pre></bdo></b><th id='zCkyg'></th></span></q></dt></tr></i><div id='zCkyg'><tfoot id='zCkyg'></tfoot><dl id='zCkyg'><fieldset id='zCkyg'></fieldset></dl></div>