我有一个Dictionary<string, string>对象,它的数据应该通过在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")); }该DataRow[]阵列被正确填充,ResizeListView(int _rows)方法做它的适应名单高度作业时,ListView正确显示的列标题,但剩下的只是空行。
1 回答
- 1 回答
- 0 关注
- 207 浏览
添加回答
举报
0/150
提交
取消