Listview thành dữ liệu (Listview to datatable)


問題描述

Listview thành dữ liệu (Listview to datatable)

I have a listview and I NEED to load the rows and columns into datatable.

I have tried as follows

DataTable dt = new DataTable(); 

foreach (ListViewItem item in listView1.Items)
{
    table.Columns.Add(item.ToString()); 
    foreach (var it in item.SubItems) 
        dt.Rows.Add(it.ToString()); 
}

When I retrieved the rowcount and columncount then I got number of rows as column count and number of column as rowcount 

dont know what's going on.. please help me best regards Bunzitop


參考解法

方法 1:

its a long time ago, but someone will probably struggle about this in the future,  so here is my solution to convert a ListView to a DataTable:

DataTable dtZeitplan = new DataTable();
foreach (ColumnHeader chZeitplan in lvZeitplan.Columns)
{
    dtZeitplan.Columns.Add(chZeitplan.Text);
}
foreach (ListViewItem item in lvZeitplan.Items)
{
    DataRow row = dtZeitplan.NewRow();
    for(int i = 0; i < item.SubItems.Count; i++)
    {
        row[i] = item.SubItems[i].Text;
    }
    dtZeitplan.Rows.Add(row);
}

方法 2:

You are doing it so wrong. You need 1 + listView1.Items[0].SubItems.Count columns in your DataTable (the 1 is for ListViewItem and others are subitems) and listView1.Items.Count number of rows. Therefore your code should be like this:

if (listView1.Items.Count > 0)
{
       dt.Columns.Add();
       foreach (ListViewItem.ListViewSubItem lvsi in listView1.Items[0].SubItems)
              dt.Columns.Add();
       //now we have all the columns that we need, let's add rows
       foreach (ListViewItem item in listView1.Items)
       {
              List<string> row = new List<string>();
              row.Add(item.ToString());
              foreach (var it in item.SubItems)
                   row.Add(it.ToString());
              //Add the row into the DataTable
              dt.Rows.Add(row.ToArray());
       }
}

方法 3:

 DataTable table = new DataTable();

            table.Columns.Add("MODUL", typeof(string));
            table.Columns.Add("ACIKLAMA", typeof(string));
            table.Columns.Add("UZUNLUK", typeof(string));
            table.Columns.Add("GENISLIK", typeof(string));
            table.Columns.Add("MIKTAR", typeof(string));

            for (int i = 0; i < listView2.Items.Count; i++)
            {
                table.Rows.Add(listView2.Items[i].SubItems[1].Text, listView2.Items[i].SubItems[2].Text, listView2.Items[i].SubItems[3].Text, listView2.Items[i].SubItems[4].Text, listView2.Items[i].SubItems[5].Text);
            }

方法 4:

You can use the ItemsSource property and System.Data classes (ADO.Net) to bind a listview to a datatable and vise‑verse (what you want).  The following code will give you a datatable from an existing bound ListView control.

DataView theDataView = (DataView)theListView.ItemsSource;
DataTable theDataTable = theDataView.Table;

Manish Mishra asked what your listview control is bound to.  This is a very good question.  My answer assumes it is already bound to a datatable.

方法 5:

Working code from VB.Net: Just replace your listview name with "LVActions" 

        Dim it As Integer = 0
        Dim dt As New DataTable
        For it = 0 To LVActions.Items(0).SubItems.Count ‑ 1
            Dim DCOL As New DataColumn(LVActions.Columns(it).Text)
            dt.Columns.Add(DCOL)
        Next
        For it = 0 To LVActions.Items.Count ‑ 1
            Dim DROW As DataRow = dt.NewRow
            For j As Integer = 0 To LVActions.Items(it).SubItems.Count ‑ 1
                DROW(LVActions.Columns(j).Text) = LVActions.Items(it).SubItems(j).Text
            Next
            dt.Rows.Add(DROW)
        Next

(by BunzitopJoshiNikola DavidovicerhanarMikeGeekSunil Patil)

參考文件

  1. Listview to datatable (CC BY‑SA 3.0/4.0)

#listview #winforms #Database #datatable #C#






相關問題

ListView中的序列號列 (Serial number Column in ListView)

狀態列表可繪製在預蜂窩版本上無法正常工作 (State list drawable not working properly on pre-honeycomb versions)

我可以減少列表視圖的執行時間嗎? (Can i reduce execution time of listview?)

ListView Windows 8 多個索引 (ListView Windows 8 multiple indexes)

如何通過單擊歌曲列表來播放歌曲 (How to play the song by clicking the lists of song)

Thuộc tính chi tiết trong ListView không hiển thị (Details property in ListView not displaying)

如何從 DataModel 動態訪問數據以獲取 Blackberry 10 中的可折疊列表 (How to access data dynamically from DataModel for collapsible list in Blackberry 10)

android如何將listview項目發送到另一個活動 (android How to send listview item to another activity)

實現 RecyclerView (Implementing RecyclerView)

如何刪除列表視圖底部的多餘空間? (How can I remove extraspace in listview bottom?)

ListViewItem ItemSelectionChangedEvent 觸發 4 次 [e.Selected 觸發兩次] 導致 Win32 異常未處理 (ListViewItem ItemSelectionChangedEvent Fires 4 times [e.Selected fires twice] leads to Win32 Exception Unhandled)

Kivy:如何使用 RecycleView 在 Kivy 中顯示具有可變行的列表? (Kivy: how to display a list with variable rows in Kivy with a RecycleView?)







留言討論