【发布时间】: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
提前致谢!
丹
【问题讨论】: