I have a list of 2 DataTables. I want to iterate through each of them, one at a time. How do I do this? There are 0 examples of going through a list of DataTables.
List<DataTable> test = new List<DataTable>();
foreach (DataRow dataRow in TEST.LIST)
{
string value = dataRow.Field<string>("Slave_IO_Running"); //Looks for "Slave_IO_Running" status.
if (value == "Yes")
{
results.Add(siteName + ": WORKING"); //adds working to the visual table
}
else
{
results.Add(siteName + ": REPLICATION ERROR"); //adds not working to the result list
}
break;
}
for each datatable you can use DataTable.Rows and for each row you can access the properties as row["columnName"] or traverse each column in the corresponding row
like
foreach(DataTable table in tables)
{
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.WriteLine(row[column]);
}
}
}
You can try it like this, assuming both tables contain the same columns:
foreach (DataRow dataRow in test.SelectMany(dt => dt.Rows.OfType<DataRow>()))
{
// your code using the rows
}
Related
The code below choose the first row and remove the other row below with specific id. The things that i want is the last line within the id.
var dtremove = RemoveDuplicateRows(dt, "id);
This the extension
public DataTable RemoveDuplicateRows(DataTable dt, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dt.Rows)
{
if (hTable.Contains(drow[colName]))
{
duplicateList.Add(drow);
}
else
hTable.Add(drow[colName], string.Empty);
}
//duplicateList.Sort();
//Removing a list of duplicate items from datatable.
foreach (DataRow dRow in duplicateList)
dt.Rows.Remove(dRow);
//Datatable which contains unique records will be return as output.
return dt;
}
The example of datatable.
id date
------------
A 1/1/2018
A 1/2/2018
A 1/3/2018
B 2/1/2018
B 2/2/2018
i want the result like this.
id date
------------
A 1/3/2018
B 2/2/2018
I tried not to change too much of your code, pretty sure it can be improved, but the changes below looks for the value and if it already exists in the dictionary, it replaces it: Thus, you will end up with the very last row instead of the first one. Also, it returns a new DataTable and the original one will stay intact.
public DataTable RemoveDuplicateRows(DataTable dt, string colName)
{
var uniqueRows = new Dictionary<string, DataRow>();
foreach (DataRow thisRrow in dt.Rows)
{
if (uniqueRows.ContainsKey(colName))
{
uniqueRows[colName] = thisRrow;
}
else
{
uniqueRows.Add(colName, thisRrow);
}
}
DataTable copy = dt.Copy();
copy.Rows.Clear();
foreach (var thisRow in uniqueRows)
{
copy.Rows.Add(thisRow.Value);
}
//Datatable which contains unique records will be return as output.
return copy;
}
I want to add table name as string and table value as arraylist if table name exist in same key add record as array list ...here showing key already exist . where i can modify :
SortedDictionary<string, ArrayList> DealInfo = new SortedDictionary<string, ArrayList>();
ArrayList Deal = new ArrayList();
string DealName=string.Empty;
foreach (DataTable table in RecentAddedDeal.Tables)
{
foreach (DataRow dr in table.Rows)
{
if (!DealInfo.ContainsKey(Convert.ToString(dr["DealTab"])))
{
DealName = Convert.ToString(dr["DealTab"]);
}
Deal.Add(dr);
DealInfo.Add(DealName, Deal);
}
}
It's hard to tell exactly what you want since your description says you want to use the TableName as the key, but your code is adding a column from the row as a key.
Going with the idea that the TableName should be the key, something like this should work:
var dealInfo = new SortedDictionary<string, List<DataRow>>();
foreach (DataTable table in RecentAddedDeal.Tables)
{
if (!dealInfo.ContainsKey(table.TableName))
{
dealInfo.Add(table.TableName, table.Rows.Cast<DataRow>().ToList());
}
}
Going with the idea that you want to key off a column in each row for the key, you could do something like:
var dealInfo = new SortedDictionary<string, List<DataRow>>();
foreach (DataTable table in RecentAddedDeal.Tables)
{
foreach (DataRow row in table.Rows)
{
var dealName = row["DealTab"].ToString();
if (dealInfo.ContainsKey(dealName))
{
dealInfo[dealName].Add(row);
}
else
{
dealInfo.Add(dealName, new List<DataRow> {row});
}
}
}
To fill a ListView with data from a row for a specific DealName, you can find the dictionary entry for that deal name and access the list of rows this way:
foreach (DataRow row in dealInfo["SomeDealName"])
{
// Here you have access to the rows where row["DealTab"] == "SomeDealName"
// You can fill a list view with some column value from the row like:
listView1.Add(row["SomeColumnName"].ToString());
}
i have two datatable arrays
DataTable[] DTrightSplitH2;
DataTable[] DTleftSplitH2;
what i try to do is to take each datatable in DTright and compare to DTleft in "key" columns
it they are the same, merge the row
i know i should use DataTable.Merge with bool set to false and adding missing schema but i can't make it work like i want
Try this :
DTrightSplitH2.Union(DTleftSplitH2);
Sounds as if you could use my MergeAll method (usage below).
public static DataTable MergeAll(this IList<DataTable> tables, String primaryKeyColumn)
{
if (!tables.Any())
throw new ArgumentException("Tables must not be empty", "tables");
if(primaryKeyColumn != null)
foreach(DataTable t in tables)
if(!t.Columns.Contains(primaryKeyColumn))
throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");
if(tables.Count == 1)
return tables[0];
DataTable table = new DataTable("TblUnion");
table.BeginLoadData(); // Turns off notifications, index maintenance, and constraints while loading data
foreach (DataTable t in tables)
{
foreach (DataColumn col in t.Columns)
col.ReadOnly = false; // required e.g. if you use a DataSet with Foreign-Key Constraints
table.Merge(t); // same as table.Merge(t, false, MissingSchemaAction.Add);
}
table.EndLoadData();
if (primaryKeyColumn != null)
{
// since we might have no real primary keys defined, the rows now might have repeating fields
// so now we're going to "join" these rows ...
var pkGroups = table.AsEnumerable()
.GroupBy(r => r[primaryKeyColumn]);
var dupGroups = pkGroups.Where(g => g.Count() > 1);
foreach (var grpDup in dupGroups)
{
// use first row and modify it
DataRow firstRow = grpDup.First();
foreach (DataColumn c in table.Columns)
{
if (firstRow.IsNull(c))
{
DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
if (firstNotNullRow != null)
firstRow[c] = firstNotNullRow[c];
}
}
// remove all but first row
var rowsToRemove = grpDup.Skip(1);
foreach(DataRow rowToRemove in rowsToRemove)
table.Rows.Remove(rowToRemove);
}
}
return table;
}
Usage:
var tables = DTrightSplitH2.Concat(DTleftSplitH2).ToArray();
DataTable TblUnion = tables.MergeAll("key");
I have a DataSet named DataSet1. It contains an unknown number of tables and an unknown number of columns and rows in those tables. I would like to loop through each table and look at all of the data in each row for each column. I'm not sure how to code this. Any help would be appreciated!
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (object item in row.ItemArray)
{
// read item
}
}
}
Or, if you need the column info:
foreach (DataTable table in dataSet.Tables)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
object item = row[column];
// read column and item
}
}
}
Just loop...
foreach(var table in DataSet1.Tables) {
foreach(var col in table.Columns) {
...
}
foreach(var row in table.Rows) {
object[] values = row.ItemArray;
...
}
}
I may well be looking at this problem backwards but I am curious none the less. Is there a way to build a DataTable from what is currently displayed in the DataGridView?
To be clear, I know you can do this DataTable data = (DataTable)(dgvMyMembers.DataSource); however that includes hidden columns. I would like to build it from the displayed columns only.
Hope that makes sense.
So I ended up trying a combination of a couple of answers as that seemed best. Below is what I am trying. Basically I am creating the DataTable from the DataSource and then working backwards based on if a column is visible or not. However, after it removes a column I get a Collection was modified; enumeration operation may not execute on the next iteration of the foreach.
I am confused as I am not trying to modify the DataGridView, only the DataTable so what's up?
DataTable data = GetDataTableFromDGV(dgvMyMembers);
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = ((DataTable)dgv.DataSource).Copy();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (!column.Visible)
{
dt.Columns.Remove(column.Name);
}
}
return dt;
}
Well, you can do
DataTable data = (DataTable)(dgvMyMembers.DataSource);
and then use
data.Columns.Remove(...);
I think it's the fastest way. This will modify data source table, if you don't want it, then copy of table is reqired. Also be aware that DataGridView.DataSource is not necessarily of DataTable type.
I don't know anything provided by the Framework (beyond what you want to avoid) that would do what you want but (as I suspect you know) it would be pretty easy to create something simple yourself:
private DataTable GetDataTableFromDGV(DataGridView dgv) {
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns) {
if (column.Visible) {
// You could potentially name the column based on the DGV column name (beware of dupes)
// or assign a type based on the data type of the data bound to this DGV column.
dt.Columns.Add();
}
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows) {
for (int i = 0; i < row.Cells.Count; i++) {
cellValues[i] = row.Cells[i].Value;
}
dt.Rows.Add(cellValues);
}
return dt;
}
one of best solution enjoyed it ;)
public DataTable GetContentAsDataTable(bool IgnoreHideColumns=false)
{
try
{
if (dgv.ColumnCount == 0) return null;
DataTable dtSource = new DataTable();
foreach (DataGridViewColumn col in dgv.Columns)
{
if (IgnoreHideColumns & !col.Visible) continue;
if (col.Name == string.Empty) continue;
dtSource.Columns.Add(col.Name, col.ValueType);
dtSource.Columns[col.Name].Caption = col.HeaderText;
}
if (dtSource.Columns.Count == 0) return null;
foreach (DataGridViewRow row in dgv.Rows)
{
DataRow drNewRow = dtSource.NewRow();
foreach (DataColumn col in dtSource .Columns)
{
drNewRow[col.ColumnName] = row.Cells[col.ColumnName].Value;
}
dtSource.Rows.Add(drNewRow);
}
return dtSource;
}
catch { return null; }
}
First convert you datagridview's data to List, then convert List to DataTable
public static DataTable ToDataTable<T>( this List<T> list) where T : class {
Type type = typeof(T);
var ps = type.GetProperties ( );
var cols = from p in ps
select new DataColumn ( p.Name , p.PropertyType );
DataTable dt = new DataTable();
dt.Columns.AddRange(cols.ToArray());
list.ForEach ( (l) => {
List<object> objs = new List<object>();
objs.AddRange ( ps.Select ( p => p.GetValue ( l , null ) ) );
dt.Rows.Add ( objs.ToArray ( ) );
} );
return dt;
}