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

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

问题描述

我喜欢能在windows窗体来查看数据表

我设法只ListView控件如何在那里得到的数据,以获得标题

  DataTable的数据=新的DataTable();数据= EnumServices();//创建头
的foreach(在data.Columns的DataColumn列)
{
      listView_Services.Columns.Add(column.ColumnName);
}

我只是想现在展现在那里的数据!

欢呼声


解决方案

 的foreach(DataRow的在data.Rows行)
{
    ListViewItem的项目=新的ListViewItem(行[0]的ToString());
    的for(int i = 1; I< data.Columns.Count;我++)
    {
        item.SubItems.Add(行[I]的ToString());
    }
    listView_Services.Items.Add(项目);
}

更新:同时,如果你调用你的方法不止一次,你需要添加列之前要么清除列集合,或者检查是否列已添加 - 否则,列数将不断增加每次调用你的方法的时间。

I like to be able to view datatable in windows form

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!

cheers

解决方案

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天全站免登陆