Listview to datatable - c#

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

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);
}

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());
}
}

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);
}

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.

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

Related

How to programmatically add rows to DataGrid in C#?

So as the title states, I'm trying to add rows to a DataGrid programmatically using C# but I can't seem to make it work. This is what I have so far.
// I have a DataGrid declared as dg in the XAML
foreach (string s in array) {
int index = 0;
DataGridRow dgRow = new DataGridRow();
foreach (DataGridColumn dgColumn in columns)
{
// Trying to add Text to dgRow here but IDK how
index++;
}
}
I've been googling around and the closest I got to adding anything was to use {column = value} but it just throws me an error. Really out of ideas now though :\
Here's you can do it better way by binding a source to datagridview
// Creating DataSource here as datatable having two columns
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name");
// Adding the rows in datatable
for (int iCount = 1; iCount < 6; iCount++)
{
var row = dt.NewRow();
row["ID"] = iCount;
row["Name"] = "Name " + iCount;
dt.Rows.AddRow(row);
}
DataGridView dgv = new DataGridView();
// Set AutoGenerateColumns true to generate columns as per datasource.
dgv.AutoGenerateColumns = true;
// Finally bind the datasource to datagridview.
dgv.DataSource = dt;
In Case you are using WPF DataGrid
you can bind it like this way-
dgv.DataContext = employeeData.DefaultView;
and in
XAML
<DataGrid Name="dgv" ItemsSource="{Binding}">
//create datatable and columns,
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Column 1"));
dtable.Columns.Add(new DataColumn("Column 2"));
//simple way create object for rowvalues here i have given only 2 add as per your requirement
object[] RowValues = { "", "" };
//assign values into row object
RowValues[0] = "your value 1";
RowValues[1] = "your value 2";
//create new data row
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();
//now bind datatable to gridview...
gridview.datasource=dbtable;
gridview.databind();
Regards
did you try?:
int n=5; // number of rows you want to add
dataGridView1.Rows.Add(n);
// you can add (names of the rows) if you have them in your array
//for(int i=0; i<n; i++)
//dataGridView1[0, i].Value = array[i];

How to store data grid into datatable?

I need to store datagridview selected rows into datatable in single columns like below
DataGridView
user1 user2 user3
ram sam ravi
DataTable
values
ram
sam
ravi
I tried below code
DataTable dt = new DataTable();
dt.Columns.Add("values",typeof(string));
foreach (DataGridViewRow gridRow in dataGridView_settings.Rows)
{
DataRow dtRow = dt.NewRow();
for (int i = 0; i < dataGridView_settings.Columns.Count; i++)
{
dtRow[0] = gridRow.Cells[i].Value;
dt.Rows.Add(dtRow);
}
}
Problem is its adding only one row in the datatable then showing error "This row already belongs to this table."
You should move the DataRow creation to the inner for
DataTable dt = new DataTable();
dt.Columns.Add("values",typeof(string));
foreach (DataGridViewRow gridRow in dataGridView_settings.Rows)
{
for (int i = 0; i < dataGridView_settings.Columns.Count; i++)
{
DataRow dtRow = dt.NewRow();
dtRow[0] = gridRow.Cells[i].Value;
dt.Rows.Add(dtRow);
}
}

Delete Row From Data Table

I want to Delete the Multiple records from the DataTable
For example :
in my case PaperId is Repeating several Times.I want to Delete it all Duplicate records.
i have written code but loop is giving error
DataSet ds = new DataSet();
sqlDad.Fill(ds);
DataTable dt1 = new DataTable();
ds.Tables.Add(dt1);
dt1 = ds.Tables[0];
DataTable dt2 = new DataTable();
dt2 = dt1;
List<DataRow> rowsToDelete = new List<DataRow>();
foreach(DataRow dr in ds.Tables[0].Rows)
{
int r = ds.Tables[0].Columns.Count;
string x = dr.ItemArray[0].ToString();
int counter = 0;
foreach (DataRow dr1 in ds.Tables[0].Rows)
{
if (x == dr1.ItemArray[0].ToString())
{
counter++;
}
if (counter > 1)
{
rowsToDelete.Add(dr1);
foreach (DataRow row in rowsToDelete)
{
dt2.Rows.Remove(row);
}
dt2.AcceptChanges();
rowsToDelete.Clear();
}
}
Using the DefaultView of the DataTable and setting the sort order on the column that you don't want repeats to appear. You could loop over the rows and delete all the rows after the first one
// Work on the first table of the DataSet
DataTable dt1 = ds.Tables[0];
// No need to work if we have only 0 or 1 rows
if(dt1.Rows.Count <= 1)
return;
// Setting the sort order on the desidered column
dt1.DefaultView.Sort = dt1.Columns[0].ColumnName;
// Set an initial value ( I choose an empty string but you could set to something not possible here
string x = string.Empty;
// Loop over the row in sorted order
foreach(DataRowView dr in dt1.DefaultView)
{
// If we have a new value, keep it else delete the row
if(x != dr[0].ToString())
x = dr[0].ToString();
else
dr.Row.Delete();
}
// Finale step, remove the deleted rows
dt1.AcceptChanges();
Try This
DataRow[] rows;
rows=dataTable.Select("UserName = 'ABC'"); // UserName is Column Name
foreach(DataRow r in rows)
r.Delete();
If you want to remove the entire row from DataTable ,
try this
DataTable dt = new DataTable(); //User DataTable
DataRow[] rows;
rows = dt.Select("UserName = 'KarthiK'");
foreach (DataRow row in rows)
dt.Rows.Remove(row);

Looping through a DataTable

Well. I have a DataTable with multiple columns and multiple rows.
I want to loop through the DataTable dynamically basically the output should look as follows excluding the braces :
Name (DataColumn)
Tom (DataRow)
Peter (DataRow)
Surname (DataColumn)
Smith (DataRow)
Brown (DataRow)
foreach (DataColumn col in rightsTable.Columns)
{
foreach (DataRow row in rightsTable.Rows)
{
//output
}
}
I typed that out and noticed this would not work. Can someone please advice on a better way of doing this?
foreach (DataColumn col in rightsTable.Columns)
{
foreach (DataRow row in rightsTable.Rows)
{
Console.WriteLine(row[col.ColumnName].ToString());
}
}
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
}
Please try the following code below:
//Here I am using a reader object to fetch data from database, along with sqlcommand onject (cmd).
//Once the data is loaded to the Datatable object (datatable) you can loop through it using the datatable.rows.count prop.
using (reader = cmd.ExecuteReader())
{
// Load the Data table object
dataTable.Load(reader);
if (dataTable.Rows.Count > 0)
{
DataColumn col = dataTable.Columns["YourColumnName"];
foreach (DataRow row in dataTable.Rows)
{
strJsonData = row[col].ToString();
}
}
}
If you want to change the contents of each and every cell in a datatable then we need to Create another Datatable and bind it as follows using "Import Row". If we don't create another table it will throw an Exception saying "Collection was Modified".
Consider the following code.
//New Datatable created which will have updated cells
DataTable dtUpdated = new DataTable();
//This gives similar schema to the new datatable
dtUpdated = dtReports.Clone();
foreach (DataRow row in dtReports.Rows)
{
for (int i = 0; i < dtReports.Columns.Count; i++)
{
string oldVal = row[i].ToString();
string newVal = "{"+oldVal;
row[i] = newVal;
}
dtUpdated.ImportRow(row);
}
This will have all the cells preceding with Paranthesis({)

How to bind DataRow to the GridView?

I have one datatable which has four or five columns. I dont know exactly the columns name and its count. But I want to bind the first row of the datatable into the GridView. How to do this? I need all your suggestions please.
Linq should be helpful here to get first item.
var Temp = dt.AsEnumerable().Take(1).CopyToDataTable();
use the filter in the datatable :
dt.Select("ID = 1");
you can try like this..
dt = new DataTable();
dt_Property.Columns.Add("Field1");
int i = 0;
DataRow row = null;
foreach (DataRow r in ds.Tables[0].Rows)
{
row = dt.NewRow();
row["Field1"] = ds.Tables[0].Rows[i][1];
dt_Property.Rows.Add(row);
i = i + 1;
}
dataGridView1.DataSource = dt;

Categories

Resources