【问题标题】:How to keep multiple listviews' items selected in a Win forms app如何在 Win forms 应用程序中保持选择多个列表视图的项目
【发布时间】:2014-06-03 16:17:45
【问题描述】:

我希望能为你们这些聪明聪明的人提供一个简单的解决方案。我有一个 Windows 窗体应用程序 (C#),它有 2 个列表视图。用户将从 listview1 中选择至少一项(我们将其称为 people),并从 listview2 中选择至少一项(我们将其称为 office)。关键是将人员分配到办公室并将其保存在数据库中。

问题在于,一旦他们从一个列表视图中选择了一个项目并单击另一个列表视图上的任何内容,它就会从初始列表视图中清除显示的选项。我知道我可以以编程方式获取信息,但用户会认为他们从第一个列表视图中的选择被删除了。关于如何保持第一个列表视图上的选择可见的任何想法?

更新:这是迄今为止应用程序的屏幕截图:(它说我需要 1`0 代表才能发布图像,所以我将链接到它) http://s17.postimg.org/wcajg7o4v/Untitled.png

用户点击一个办公室,然后点击一个案例,我在数据库中创建一个条目以保留他们的 ID。问题是,如果我单击一个办公室,然后在另一个列表视图(案例)中执行任何操作,那么我在办公室中选择的项目将不再可见。该代码超过 1500 行并且还在增加,所以我不确定您需要什么代码。我对列表视图使用的唯一操作是排序方法并填充它们。这是该代码(或其中一些)。 loadOfficeList 方法填充了办公室列表视图,它下面的类是我对所有列表视图的排序类(当然还有它们各自的对象)。

        private void loadOfficeList()
    {
        listViewOffices.Clear();
        listViewOffices.Items.Clear();
        DataTable t = new DataTable();
        using (SqlConnection c = new SqlConnection(connectionString))
        {
            c.Open();
            // 2
            // Create new DataAdapter
            using (SqlDataAdapter a = new SqlDataAdapter("select ID, Name from Offices where deleted = 'false'", c))
            {
                DataSet ds = new DataSet();
                a.Fill(ds);
                t = ds.Tables[0];
            }//end inner using
        }//end outer using

        // Set DataSource property of ListBox as DataSet's DefaultView

        this.listViewOffices.TabIndex = 0;
        this.listViewOffices.View = System.Windows.Forms.View.List;
        ColumnHeader colHead;

        colHead = new ColumnHeader();
        colHead.Width = 250;
        colHead.Text = "Office Name";
        this.listViewOffices.Columns.Add(colHead);


        colHead = new ColumnHeader();
        colHead.Width = 40;
        colHead.Text = "ID";
        this.listViewOffices.Columns.Add(colHead);

        ListViewItem lvi;
        ListViewItem.ListViewSubItem lvsi;

        foreach (DataRow dataRow in t.Rows)
        {

            lvi = new ListViewItem();
            lvi.Text = dataRow["Name"].ToString();
            lvi.Tag = dataRow["Name"].ToString();

            lvsi = new ListViewItem.ListViewSubItem();
            lvsi.Text = dataRow["ID"].ToString();
            lvi.SubItems.Add(lvsi);


            this.listViewOffices.Items.Add(lvi);
            this.listViewOffices.EndUpdate();

            this.listViewOffices.View = System.Windows.Forms.View.Details;

        }//end foreach

    }//end method

public class ListViewColumnSorter : IComparer
{
    // Specifies the column to be sorted
    private int ColumnToSort;

    // Specifies the order in which to sort (i.e. 'Ascending').
    private SortOrder OrderOfSort;

    // Case insensitive comparer object
    private CaseInsensitiveComparer ObjectCompare;


    // Class constructor.  Initializes various elements
    public ListViewColumnSorter()
    {
        // Initialize the column to '0'
        ColumnToSort = 0;

        // Initialize the sort order to 'none'
        OrderOfSort = SortOrder.None;

        // Initialize the CaseInsensitiveComparer object
        ObjectCompare = new CaseInsensitiveComparer();
    }//end constructor


    // This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
    // <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
    public int Compare(object x, object y)
    {
        int compareResult;
        ListViewItem listviewX, listviewY;

        // Cast the objects to be compared to ListViewItem objects
        listviewX = (ListViewItem)x;
        listviewY = (ListViewItem)y;

        compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text);

        // Calculate correct return value based on object comparison
        if (OrderOfSort == SortOrder.Ascending)
            return compareResult;
        else if (OrderOfSort == SortOrder.Descending)
            return (-compareResult);
        else
            return 0;
    }//end method

    // Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
    public int SortColumn
    {
        set
        { ColumnToSort = value; }
        get
        { return ColumnToSort; }
    }//end method

    // Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
    public SortOrder Order
    {
        set
        { OrderOfSort = value; }
        get
        { return OrderOfSort; }
    }//end method

}//end class

提前致谢!

【问题讨论】:

    标签: c# .net winforms listview


    【解决方案1】:

    根据您的描述,这听起来像是 HideSelection 属性的工作?

    来自 MSDN:

    ListView.HideSelection 属性

    获取或设置一个值,该值指示当控件失去焦点时控件中的选定项是否保持突出显示。

    如果控件失去焦点时所选项目未突出显示,则为true;如果控件失去焦点时所选项目仍突出显示,则为 false。默认为真。

    【讨论】:

    • 嗨!如果用户单击其他项目,则确实会保留选择,但如果您单击另一个列表视图,则不会保留选择。不过感谢您的帮助!
    • @user2883140 也许您应该发布您的代码和/或您正在谈论的内容的屏幕截图?我刚刚创建了一个临时项目,其中包含两个带有虚拟项目的列表视图,将它们的 HideSelection 属性设置为 false,运行它,在第一个项目中选择一个项目,然后单击并在第二个项目中选择一个项目,然后项目在第一个中仍然显示为选中状态(尽管变灰表示它没有焦点),并且每个中选中的项目数也是正确的?
    • 更新了屏幕截图和我的代码以及更多详细信息。感谢所有帮助我的人!
    • 对于遇到此问题的任何人,我只是在其中一个列表视图中添加了一个复选框字段并解决了我的问题:this.listView1.CheckBoxes = true;
    【解决方案2】:

    为什么不使用列表框。我做了以下测试用例,它适用于列表框

    listBox1.Items.Add("listbox1 item1");
    listBox1.Items.Add("listbox1 item2");
    listBox1.Items.Add("listbox1 item3");
    listBox1.Items.Add("listbox1 item4");
    
    
    
    listBox2.Items.Add("listbox2 item1");
    listBox2.Items.Add("listbox2 item2");
    listBox2.Items.Add("listbox2 item3");
    listBox2.Items.Add("listbox2 item4");
    
    
    listView1.Items.Add("listview1 item1");
    listView1.Items.Add("listview1 item2");
    listView1.Items.Add("listview1 item3");
    listView1.Items.Add("listview1 item4");
    
    
    
    listView2.Items.Add("listview2 item1");
    listView2.Items.Add("listview2 item2");
    listView2.Items.Add("listview2 item3");
    listView2.Items.Add("listview2 item4");
    

    ![示例截图]:http://i.stack.imgur.com/KZiK6.png

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多