Reputation: 204
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
Upvotes: 2
Views: 6782
Reputation: 857
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
Upvotes: 0
Reputation: 21
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.
Upvotes: 0
Reputation: 168
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);
}
Upvotes: 1
Reputation: 1
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);
}
Upvotes: 0
Reputation: 8656
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());
}
}
Upvotes: 0