C# 数据表到列表视图 [英] C# datatable to listview

查看:23
本文介绍了C# 数据表到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢能够以windows形式查看数据表

I like to be able to view datatable in windows form

我设法仅使用 ListView 获取标题,如何在其中获取数据

I managed to get the headers only with ListView how to get the data in there

DataTable data = new DataTable();

data = EnumServices();

//create headers
foreach (DataColumn column in data.Columns)
{
      listView_Services.Columns.Add(column.ColumnName);
}

我现在只想显示里面的数据!

I just want to show now the data in there!

干杯

推荐答案

foreach (DataRow row in data.Rows)
{
    ListViewItem item = new ListViewItem(row[0].ToString());
    for (int i = 1; i < data.Columns.Count; i++)
    {
        item.SubItems.Add(row[i].ToString());
    }
    listView_Services.Items.Add(item);
}

更新:此外,如果您多次调用您的方法,您需要在添加列之前清除列集合,或者检查列是否已添加 -否则,每次调用方法时,列数都会不断增加.

Update: also, if you're calling your method more than once, you need to either clear the columns collection before adding the columns, or check to see if the columns have already been added - otherwise, the number of columns will keep increasing every time you call your method.

这篇关于C# 数据表到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆