有没有一种简单的方法可以通过 DataTable 填充 ListView?

发布于 2024-10-10 09:28:30 字数 296 浏览 0 评论 0原文

DataGridView 有一个属性“DataSource”,可以简单地分配给 DataTable 来填充它。这意味着我们不必担心 DataTable 中的名称/列数。

但是,我还没有找到填充 ListView 的类似方法。似乎您需要知道 DataTable 有多少列以及每列的名称才能执行此操作,这使得它变得更加困难。

谁能建议一种简单的方法来填充 ListView 就像我们使用 DataGridView 一样?

The DataGridView has a property "DataSource" that can simply be assigned to a DataTable to populate it. This means we don't have to worry about the names/number of columns in the DataTable.

But, I haven't found a similar way of populating a ListView. It seems that you need to know how many columns the DataTable has and the names of each in order to do this, making it much more difficult.

Can anyone suggest an easy way to populate a ListView like we can with a DataGridView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-10-17 09:28:30
private void LoadList()
{
    // Get the table from the data set
    DataTable dtable = _DataSet.Tables["Titles"];

    // Clear the ListView control
    listView1.Items.Clear();

    // Display items in the ListView control
    for (int i = 0; i < dtable.Rows.Count; i++)
    {
        DataRow drow = dtable.Rows[i];

        // Only row that have not been deleted
        if (drow.RowState != DataRowState.Deleted)
        {
            // Define the list items
            ListViewItem lvi = new ListViewItem(drow["title"].ToString());
            lvi.SubItems.Add (drow["title_id"].ToString());
            lvi.SubItems.Add (drow["price"].ToString());
            lvi.SubItems.Add (drow["pubdate"].ToString());

            // Add the list items to the ListView
            listView1.Items.Add(lvi);
        }
    }
}

还可以在 - 处找到排序等内容
http://www.akadia.com/services/dotnet_listview_sort_dataset.html

修改 -

// Clear the ListView control
            listView1.Items.Clear();
            int ColCount = dtable.Columns.Count;
            //Add columns
            for (int k = 0; k < ColCount; k++)
            {
               listView1.Columns.Add(dtable.Columns[k].ColumnName);
            }
            // Display items in the ListView control
            for (int i = 0; i < dtable.Rows.Count; i++)
            {
                DataRow drow = dtable.Rows[i];

                // Only row that have not been deleted
                if (drow.RowState != DataRowState.Deleted)
                {
                    // Define the list items
                    ListViewItem lvi = new ListViewItem(drow[0].ToString());
                    for (int j = 1; j < ColCount; j++)
                    {
                        lvi.SubItems.Add(drow[j].ToString());                        
                    }
                    // Add the list items to the ListView
                    listView1.Items.Add(lvi);
                }
            }
private void LoadList()
{
    // Get the table from the data set
    DataTable dtable = _DataSet.Tables["Titles"];

    // Clear the ListView control
    listView1.Items.Clear();

    // Display items in the ListView control
    for (int i = 0; i < dtable.Rows.Count; i++)
    {
        DataRow drow = dtable.Rows[i];

        // Only row that have not been deleted
        if (drow.RowState != DataRowState.Deleted)
        {
            // Define the list items
            ListViewItem lvi = new ListViewItem(drow["title"].ToString());
            lvi.SubItems.Add (drow["title_id"].ToString());
            lvi.SubItems.Add (drow["price"].ToString());
            lvi.SubItems.Add (drow["pubdate"].ToString());

            // Add the list items to the ListView
            listView1.Items.Add(lvi);
        }
    }
}

Find sorting etc also at -
http://www.akadia.com/services/dotnet_listview_sort_dataset.html

Modified -

// Clear the ListView control
            listView1.Items.Clear();
            int ColCount = dtable.Columns.Count;
            //Add columns
            for (int k = 0; k < ColCount; k++)
            {
               listView1.Columns.Add(dtable.Columns[k].ColumnName);
            }
            // Display items in the ListView control
            for (int i = 0; i < dtable.Rows.Count; i++)
            {
                DataRow drow = dtable.Rows[i];

                // Only row that have not been deleted
                if (drow.RowState != DataRowState.Deleted)
                {
                    // Define the list items
                    ListViewItem lvi = new ListViewItem(drow[0].ToString());
                    for (int j = 1; j < ColCount; j++)
                    {
                        lvi.SubItems.Add(drow[j].ToString());                        
                    }
                    // Add the list items to the ListView
                    listView1.Items.Add(lvi);
                }
            }
灰色世界里的红玫瑰 2024-10-17 09:28:30

ObjectListView —— .NET WinForms ListView 的开源包装器 —— 有一个DataListView 子类正是这样做的。

您可以给它一个数据源(可以是DataView
数据表,
数据集
DataViewManager,或
BindingSource),它会自动创建列并填充行,以创建功能齐全的列表视图。你只需要一行代码:

this.dataListView1.DataSource = ds1.Tables["Persons"];

ObjectListView -- an open source wrapper around a .NET WinForms ListView -- has a DataListView subclass which does exactly this.

You can give it a data source (which can be a DataView,
DataTable,
DataSet,
DataViewManager, or
BindingSource), and it will auto create columns and fill in the rows, to make fully functional list view. You just need one line of code:

this.dataListView1.DataSource = ds1.Tables["Persons"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文