我有Dictionary<string, string>
个obejct,其数据应通过TextBox
控件中提交的文本实时过滤,然后显示在ListView
控件中。
我想出的解决方案是:
将Dictionary<string, string>
转换为DataTable
使用文本框的TextChanged
事件触发过滤器
将过滤后的记录放入DataRow[]
数组,将所述数组作为参数传递给自定义方法以填充ListView
对象
现在,这是我转换字典的方式:
static DataTable dtDepartments { get; set; }
static Dictionary<string, string> GetDepartments(string _depNum, out bool _isOff)
{
// retrieve the list from a SQL Server DB
}
public myForm()
{
InitializeComponent();
Dictionary<string, string> Departments = new Dictionary<string, string>();
Departments = GetDepartments(string.Empty, out bool _isOff);
dtDepartments = new DataTable();
dtDepartments.TableName = "DEPARTMENTS";
DataColumn idColumn = dtDepartments.Columns.Add("NUM", typeof(string));
dtDepartments.Columns.Add("NAME", typeof(string));
dtDepartments.PrimaryKey = new DataColumn[] { idColumn };
foreach(KeyValuePair<string,string> kvP in Departments)
{
dtDepartments.Rows.Add(new object[] { kvP.Key, kvP.Value });
}
}
这就是我在事件方法
中填充列表的方法private void txtFilter_TextChanged(object sender, EventArgs e)
{
string filter = "NAME LIKE '%" + txtFilter.Text + "%'";
DataRow[] foundRows = dtDepartments.Select(filter);
if (foundRows.Length > 0)
{
lvDepartments.Visible = true;
ResizeListView(foundRows.Length);
PopulateListView(foundRows);
}
}
void ResizeListView(int _rows)
{
lvDepartments.Height = Math.Min(25 + (20 * _rows), 205);
}
void PopulateListView(DataRow[] _foundRows)
{
lvDepartments.Items.Clear();
ListViewItem depNum = new ListViewItem("NUM", 0);
ListViewItem depName = new ListViewItem("NAME", 0);
foreach (DataRow row in _foundRows)
{
depNum.SubItems.Add(row.Field<string>("NUM"));
depName.SubItems.Add(row.Field<string>("NAME"));
}
lvDepartments.Columns.Add("Number", -2, HorizontalAlignment.Left);
lvDepartments.Columns.Add("Name", -2, HorizontalAlignment.Left);
lvDepartments.Items.AddRange(new ListViewItem[] { depNum, depName });
}
DataRow[]
数组已正确填充,ResizeListView(int _rows)
方法在调整列表高度方面发挥作用,ListView
正确显示列标题,但其余只是空白行。
我在MSDN上关注these instructions,但我找不到我错过的内容。
非常感谢任何建议。
谢谢 -
答案 0 :(得分:1)
您似乎每列添加一个ListViewItem,每行添加一个SubItem。它反过来了。
第一列是从ListViewItem Text属性填充的。第二个至第n列从提供给SubItems.Add()
的字符串中填充。
以上假设您使用的是详细信息View模式,这是唯一允许多列的视图。
void PopulateListView(DataRow[] _foundRows)
{
lvDepartments.Items.Clear();
foreach (var row in _foundRows)
{
var item = new ListViewItem { Text = row.Field<String>("NUM") };
item.SubItems.Add( row.Field<String>("NAM") );
lvDepartments.Items.Add( item );
}
}
或者如果你是LINQ-y
void PopulateListView(DataRow[] _foundRows)
{
lvDepartments.Items.Clear();
lvDepartments.Items.AddRange
(
_foundRows.Cast<DataRow>().Select
(
row =>
{
var item = new ListViewItem
{
Text = row.Field<string>("NUM")
};
item.SubItems.Add( row.Field<string>("NAME") );
return item;
}
)
.ToArray()
);
}