首页 文章

填充后ListView为空白

提问于
浏览
0

我有一个 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 正确显示列 Headers ,但其余部分只是空白行 .

我在MSDN上关注了these instructions,但我错过了't really find what I' .

任何建议都非常感谢 .

谢谢-

1 回答

  • 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()
        );
    }
    

相关问题