<bdo id='K1Nfl'></bdo><ul id='K1Nfl'></ul>
  • <small id='K1Nfl'></small><noframes id='K1Nfl'>

        <legend id='K1Nfl'><style id='K1Nfl'><dir id='K1Nfl'><q id='K1Nfl'></q></dir></style></legend>

      1. <tfoot id='K1Nfl'></tfoot>

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

        在列表框中设置所选项目而不循环

        Setting selected item in a ListBox without looping(在列表框中设置所选项目而不循环)
      2. <i id='Qta4C'><tr id='Qta4C'><dt id='Qta4C'><q id='Qta4C'><span id='Qta4C'><b id='Qta4C'><form id='Qta4C'><ins id='Qta4C'></ins><ul id='Qta4C'></ul><sub id='Qta4C'></sub></form><legend id='Qta4C'></legend><bdo id='Qta4C'><pre id='Qta4C'><center id='Qta4C'></center></pre></bdo></b><th id='Qta4C'></th></span></q></dt></tr></i><div id='Qta4C'><tfoot id='Qta4C'></tfoot><dl id='Qta4C'><fieldset id='Qta4C'></fieldset></dl></div>

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

              <tfoot id='Qta4C'></tfoot>
                <tbody id='Qta4C'></tbody>
            1. <legend id='Qta4C'><style id='Qta4C'><dir id='Qta4C'><q id='Qta4C'></q></dir></style></legend>

                  <bdo id='Qta4C'></bdo><ul id='Qta4C'></ul>

                • 本文介绍了在列表框中设置所选项目而不循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个绑定到 DataTable 的多选列表框.DataTable 包含 2 列描述和值.

                  I have a multi-select listbox which I am binding to a DataTable. DataTable contains 2 columns description and value.

                  这是列表框填充代码:

                  DataTable copytable = null;
                                  copytable = GlobalTable.Copy(); // GlobalTable is a DataTable
                                  copytable.Rows[0][0] = "--ALL--";
                                  copytable.Rows[0][1] = "--ALL--";
                  
                                  breakTypeList.DataSource = copytable;
                                  this.breakTypeList.DisplayMember = copytable.Columns[0].ColumnName; // description
                                  this.breakTypeList.ValueMember = copytable.Columns[1].ColumnName; // value
                                  this.breakTypeList.SelectedIndex = -1;
                  

                  我将 description 设置为 DisplayMember,将 value 设置为 ListBox 的 ValueMember.现在根据传递的值,我需要在 ListBox 中设置所选项目.

                  I am setting description as the DisplayMember and value as the ValueMember of the ListBox. Now depending on what the value is passed I need to set the selected item in the ListBox.

                  这是我的代码:

                  ListBox lb = c as ListBox;
                  lb.SelectedValue = valuePassedByUser;
                  

                  这不起作用.因此我不得不求助于下面的代码(我循环遍历列表框中的所有项目)

                  which is not working. Hence I have to resort to the code below (where I loop through all the items in the list box)

                  for (int i = 0; i < lb.Items.Count; i++)
                              {
                                  DataRowView dr = lb.Items[i] as DataRowView;
                                  if (dr["value"].ToString() == valuePassedByUser)
                                  {
                                      lb.SelectedIndices.Add(i);
                                      break;
                                  }
                              }
                  

                  我想知道我的代码中缺少什么/错误.为什么是 lb.SelectedValue = valuePassedByUser;选择不正确的项目?

                  I would like to know what is missing/ erroneous in my code. Why is lb.SelectedValue = valuePassedByUser; selecting incorrect items?

                  推荐答案

                  好的......这是我昨天才意识到的难以理解的答案.虽然我没有在我的问题中提到一件重要的事情,但这是我的错误,因为我觉得它与手头的问题无关:

                  Ok ... here comes hard-to-digest answer which I realized only yesterday. It's my mistake though that I didn't mention one important thing in my question because I felt it is irrelevant to problem at hand:

                  数据表中的数据未排序.因此,我将列表框的 Sorted 属性设置为 true.后来我意识到当列表框甚至组合框的排序属性设置为 true 时,值成员没有正确设置.所以如果我写:

                  The data in the data table was not sorted. Hence I had set the listbox's Sorted property to true. Later I realized When the listbox's or even combo box's sorted property is set to true then the value member does not get set properly. So if I write:

                  lb.SelectedValue = valuePassedByUser;

                  它将某些其他项目设置为选中,而不是设置其值为 valuePassedByUser 的项目.简而言之,它与索引混淆.

                  it sets some other item as selected rather than settting the one whose Value is valuePassedByUser. In short it messes with the indexes.

                  例如如果我的初始数据是:

                  For e.g. if my initial data is:

                  Index   ValueMember DisplayMember
                  1          A            Apple
                  2          M            Mango
                  3          O            Orange
                  4          B            Banana
                  

                  我设置了 sorted = true.那么列表框项是:

                  And I set sorted = true. Then the listbox items are:

                  Index   ValueMember DisplayMember
                  1          A            Apple
                  2          B            Banana
                  3          M            Mango
                  4          O            Orange
                  

                  现在如果我想将 Banana 设置为选中,我运行 stmt:

                  Now if I want to set Banana as selected, I run the stmt:

                  lb.SelectedValue = "B";

                  但不是将 Banana 设置为选中,而是将 Orange 设置为选中.为什么?因为如果列表没有被排序,Banana 的索引会是 4.所以即使 Banana 的排序索引是 2 之后,它也会将索引 4 设置为选中,从而选择 Orange 而不是 Banana.

                  But instead of setting Banana as selected, it sets Orange as selected. Why? Because had the list not been sorted, index of Banana would be 4. So even though after sorting index of Banana is 2, it sets index 4 as selected, thus making Orange selected instead of Banana.

                  因此对于排序的列表框,我使用以下代码来设置选定项:

                  Hence for sorted listbox, I am using the following code to set selected items:

                  private void SetSelectedBreakType(ListBox lb, string value)
                  {
                      for (int i = 0; i < lb.Items.Count; i++)
                      {
                          DataRowView dr = lb.Items[i] as DataRowView;
                          if (dr["value"].ToString() == value)
                          {
                              lb.SelectedIndices.Add(i);
                              break;
                          }
                      }
                  }
                  

                  这篇关于在列表框中设置所选项目而不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                • <i id='D2s2C'><tr id='D2s2C'><dt id='D2s2C'><q id='D2s2C'><span id='D2s2C'><b id='D2s2C'><form id='D2s2C'><ins id='D2s2C'></ins><ul id='D2s2C'></ul><sub id='D2s2C'></sub></form><legend id='D2s2C'></legend><bdo id='D2s2C'><pre id='D2s2C'><center id='D2s2C'></center></pre></bdo></b><th id='D2s2C'></th></span></q></dt></tr></i><div id='D2s2C'><tfoot id='D2s2C'></tfoot><dl id='D2s2C'><fieldset id='D2s2C'></fieldset></dl></div>
                    1. <tfoot id='D2s2C'></tfoot>

                        <tbody id='D2s2C'></tbody>

                      <legend id='D2s2C'><style id='D2s2C'><dir id='D2s2C'><q id='D2s2C'></q></dir></style></legend>
                      • <small id='D2s2C'></small><noframes id='D2s2C'>

                            <bdo id='D2s2C'></bdo><ul id='D2s2C'></ul>