I have the following piece of code.
dt3 = new System.Data.DataTable();
foreach (DataRow sourceRow in dt2.Rows) {
DataRow destRow = dt3.NewRow();
destRow[0] = sourceRow[2];
dt3.Rows.Add(destRow);
}
And it is generating following error on line destRow[0] = sourceRow[2];
System.IndexOutOfRangeException: Cannot find column 0.
What am I doing wrong? Is there any way around it without declaring columns beforehand?
Here we create a "source" DataTable with 3 columns, and select two of those columns for the new DataTable:
DataTable srcDataTable = new DataTable();
srcDataTable.Columns.Add("Column A", typeof(string));
srcDataTable.Columns.Add("Column B", typeof(int));
srcDataTable.Columns.Add("Column C", typeof(int));
DataTable dstDataTable = new DataTable();
var desiredColumns = new[] { "Column A", "Column C" };
foreach (DataColumn col in srcDataTable.Columns)
{
if (desiredColumns.Contains(col.ColumnName))
{
dstDataTable.Columns.Add(col.ColumnName, col.DataType, col.Expression);
}
}
Now you can simply loop through the source table and copy the row data as you need it. Example for copying the rows:
foreach (DataRow srcRow in srcDataTable.Rows)
{
var newRow = dstDataTable.NewRow();
foreach (var columnName in desiredColumns)
{
newRow[columnName] = srcRow[columnName];
}
dstDataTable.Rows.Add(newRow);
}
Alternative approach using column numbers:
DataTable srcDataTable = new DataTable();
srcDataTable.Columns.Add("Column A", typeof(string));
srcDataTable.Columns.Add("Column B", typeof(int));
srcDataTable.Columns.Add("Column C", typeof(int));
DataTable dstDataTable = new DataTable();
var desiredColumns = new int[] { 0, 2 };
Dictionary<int, int> columnMap = new Dictionary<int, int>();
for (int colNum = 0; colNum < desiredColumns.Length; ++colNum)
{
columnMap[colNum] = desiredColumns[colNum];
dstDataTable.Columns.Add(srcDataTable.Columns[desiredColumns[colNum]].ColumnName, srcDataTable.Columns[desiredColumns[colNum]].DataType, srcDataTable.Columns[desiredColumns[colNum]].Expression);
}
foreach (DataRow srcRow in srcDataTable.Rows)
{
var newRow = dstDataTable.NewRow();
for (int colNum = 0; colNum < desiredColumns.Length; ++colNum)
{
newRow[colNum] = srcRow[columnMap[colNum]];
}
dstDataTable.Rows.Add(newRow);
}
This line dt3 = new System.Data.DataTable(); creates a new DataTable. But this table doesn't contain any columns yet. In fact, it's an empty table.
This line destRow[0] = sourceRow[2]; tries to set the value of the first column of your table. However, your table doesn't contain any columns yet. And this is what the error message is trying to tell you.
You have to create your column after creating the table. You can do it like this:
DataColumn idColumn = new DataColumn();
idColumn.DataType = System.Type.GetType("System.Int32");
idColumn.ColumnName = "id";
dt3.Columns.Add(idColumn);
Only after this, you will be able to put data into your first column.
Please take a look at this example in the Microsoft docs
What am I doing wrong?
Table dt3 has no column 0, because you haven't added one
Is there any way around it without declaring columns beforehand?
Even though you don't necessarily know the column name or type of dt2's column 2 at compile time, you seem to know that you definitely want column 2 from dt2 to be column 0 of dt3, so make sure you add a column to dt3 that is the same type (and giving it the same name seems reasonable too) as column 2 of dt2:
dt3 = new System.Data.DataTable();
dt3.Columns.Add(dt2.Columns[2].ColumnName, dt2.Columns[2].DataType);
foreach (DataRow sourceRow in dt2.Rows) {
DataRow destRow = dt3.NewRow();
destRow[0] = sourceRow[2];
dt3.Rows.Add(destRow);
}
You can try to use Clone() method to copy structure/columns from source table to destination table:
var dt3 = dt2.Clone();
From the docs.
Related
Here and Two Variable Store Two tables Data Now I want to Add these tables in to DataTable through Foreach Loop ?
var meterData = MeterTable(startDateTime, endDateTime);
var levelData = LevelTable(startDateTime, endDateTime);
var dataTable = new DataTable();
dataTable.Columns.Add("Meter", typeof(string));
dataTable.Columns.Add("Volume", typeof(int));
dataTable.Columns.Add("OpeningBalance", typeof(int));
dataTable.Columns.Add("IN", typeof(int));
dataTable.Columns.Add("Transfer", typeof(int));
dataTable.Columns.Add("OUT", typeof(int));
dataTable.Columns.Add("ClosingBalance", typeof(int));
// how Onward i have not any idea ?
If you want all columns from each table you can do this:
var newTable = new DataTable();
//Copy the meter table into your new table
newTable = meterData.Copy();
//Use .Merge to merge in the level table
newTable.Merge(levelData);
If you want to add new rows, you create a datarow and add it to the datatable:
// Create new DataRow objects and add to DataTable.
for(int i = 0; i < someNumberOfRows; i++)
{
row = newTable.NewRow();
row["column1"] = i;
row["column2"] = "item " + i.ToString();
newTable.Rows.Add(row);
}
If you need to use foreach, I'm assuming you want to iterate the rows in your 2 tables:
foreach(DataRow r in meterData.Rows)
{
row = newTable.NewRow();
row["someColumn"] = r["someColumn"];
row["someOtherColumn"] = r["someOtherColumn"];
newTable.Rows.Add(row);
}
you can add new row through foreach loop like this
var dataTable = new DataTable();
dataTable.Columns.Add("Meter", typeof(string));
dataTable.Columns.Add("Volume", typeof(int));
foreach (var items in Source) //source is the where you want to loop
{
DataRow row = dataTable.NewRow();
row["Meter"] = "meter";
//you can access value of source items through item.NameofTargetField like items.meter
row["Volume"] = 11;
dataTable.Rows.Add(row);
}
how to add two datatables?
(dt1=name,phone_number)
and
(dt2=address,cityname)
Result:
(dt3=name,phone_number,address,cityname)
Ruben, you will need some primary key in order to combine both tables unless you are 100% sure that both data tables have the same number of rows and they will always be stored in the correct order.
There is a similar question answered and the link is: Combining n DataTables into a Single DataTable
Here goes the content of the provided link:
"The table has repeating primary keys after the Merge because no primary-key was defined. So either specify the PK or try this method here which i've written from scratch(so it's not really tested):
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)
{
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;
}
You can call it in this way:
var tables = new[] { tblA, tblB, tblC };
DataTable TblUnion = tables.MergeAll("c1");
Used this sample data:
var tblA = new DataTable();
tblA.Columns.Add("c1", typeof(int));
tblA.Columns.Add("c2", typeof(int));
tblA.Columns.Add("c3", typeof(string));
tblA.Columns.Add("c4", typeof(char));
var tblB = new DataTable();
tblB.Columns.Add("c1", typeof(int));
tblB.Columns.Add("c5", typeof(int));
tblB.Columns.Add("c6", typeof(string));
tblB.Columns.Add("c7", typeof(char));
var tblC = new DataTable();
tblC.Columns.Add("c1", typeof(int));
tblC.Columns.Add("c8", typeof(int));
tblC.Columns.Add("c9", typeof(string));
tblC.Columns.Add("c10", typeof(char));
tblA.Rows.Add(1, 8500, "abc", 'A');
tblA.Rows.Add(2, 950, "cde", 'B');
tblA.Rows.Add(3, 150, "efg", 'C');
tblA.Rows.Add(4, 850, "ghi", 'D');
tblA.Rows.Add(5, 50, "ijk", 'E');
tblB.Rows.Add(1, 7500, "klm", 'F');
tblB.Rows.Add(2, 900, "mno", 'G');
tblB.Rows.Add(3, 150, "opq", 'H');
tblB.Rows.Add(4, 850, "qrs", 'I');
tblB.Rows.Add(5, 50, "stu", 'J');
tblC.Rows.Add(1, 7500, "uvw", 'K');
tblC.Rows.Add(2, 900, "wxy", 'L');
tblC.Rows.Add(3, 150, "yza", 'M');
tblC.Rows.Add(4, 850, "ABC", 'N');
tblC.Rows.Add(5, 50, "CDE", 'O');
After DataTable.Merge in MergeAll:
After some modifications to join the rows in MergeAll:
Update
Since this question arose in one of the comments, if the only relation between two tables is the index of a DataRow in the table and you want to merge both tables according to the index:
public static DataTable MergeTablesByIndex(DataTable t1, DataTable t2)
{
if (t1 == null || t2 == null) throw new ArgumentNullException("t1 or t2", "Both tables must not be null");
DataTable t3 = t1.Clone(); // first add columns from table1
foreach (DataColumn col in t2.Columns)
{
string newColumnName = col.ColumnName;
int colNum = 1;
while (t3.Columns.Contains(newColumnName))
{
newColumnName = string.Format("{0}_{1}", col.ColumnName, ++colNum);
}
t3.Columns.Add(newColumnName, col.DataType);
}
var mergedRows = t1.AsEnumerable().Zip(t2.AsEnumerable(),
(r1, r2) => r1.ItemArray.Concat(r2.ItemArray).ToArray());
foreach (object[] rowFields in mergedRows)
t3.Rows.Add(rowFields);
return t3;
}
Sample:
var dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("Name", typeof(string));
dt1.Rows.Add(1, "Jon");
var dt2 = new DataTable();
dt2.Columns.Add("Country", typeof(string));
dt2.Rows.Add("US");
var dtMerged = MergeTablesByIndex(dt1, dt2);
The result table contains three columns ID,Name,Country and a single row: 1 Jon US"
Source: Combining n DataTables into a Single DataTable
Last Access: 04-August-2016
First you can filter right column in to separate data datable
DataTable filterDatatable1= new DataTable();
DataTable filterDatatable2= new DataTable();
DataTable filterDatatable1= OriginaDatatable1.DefaultView.ToTable(false, "ColumnName1", "ColimnName2");
DataTable filterDatatable1= OriginaDatatable1.DefaultView.ToTable(false, "ColumnName1", "ColimnName2", "ColimnName3");
Then you can use merge function
DataTable dtAll = new DataTable();
dtAll.Merge(filterDatatable1);
dtAll.Merge(filterDatatable2);
Why does SqlDataAdapter's Fill method not allow me to add a row with the same its own rows' value as appearance? I could not success to provide a row's value that appears at the same row with filled one in DataTable.
Check this out:
using (SqlDataAdapter a = new SqlDataAdapter("SELECT SIPNO, SERINO, TARIH FROM SNOHAREKETLER WHERE Cast(TARIH as DATE) BETWEEN '2015/03/19' AND '2015/03/20' AND (TEZNO = 'T23' OR TEZNO = 'T31') AND CIKTI is null", c))
{
// 3
// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);
t.Columns.Add("MyColumn", typeof(string));
DataRow workRow;
int iGetCount = t.Rows.Count;
for (int i = 0; i <= iGetCount - 1; i++)
{
workRow = t.NewRow();
workRow["MyColumn"] = i;
t.Rows.Add(workRow);
}
// 4
// Render data onto the screen
dataGridView1.DataSource = t;
}
Here is the solution of my issue. I searched and consulted someone. My issue was trying to add a NewRow and this causes the issue.
DataTable t = new DataTable();
a.Fill(t);
DataColumn newCol = new DataColumn("NewColumn", typeof(string));
newCol.AllowDBNull = true;
t.Columns.Add(newCol);
foreach (DataRow row in t.Rows)
{
row["NewColumn"] = "With String";
}
dataGridView1.DataSource = t;
All, there are some question on this, but I can't seem to extract enough information to solve the problem for my case. I extract an unknown number of tables into SQL Server 'Tab1', 'Tab2', 'Tab3', ... , 'TabN'. The columns in these tables are different, but the row definitions are the same. I need to pull all the data in from the Server into N DataTables and then combine these to form a single DataTable. What I do currently is
int nTmpVolTabIdx = 1;
strSqlTmp = String.Empty;
using (DataTable dataTableALL = new DataTable())
{
while (true)
{
string strTmpVolName = String.Format("Tab{0}", nTmpVolTabIdx);
strSqlTmp = String.Format("SELECT * FROM [{0}];", strTmpVolName);
// Pull the data from 'VolX' into a local DataTable.
using (DataTable dataTable = UtilsDB.DTFromDB(conn, strTmpVolName, strSqlTmp, false))
{
if (dataTable == null)
break;
else
dataTableALL.Merge(dataTable);
}
nTmpVolTabIdx++;
}
...
}
This merges the DataTables but they are miss-aligned (padding blank cells onto the appended data set). I could append the columns of the new DataTable via a loop; but is there an easier/nicer way to do this (perhaps using LINQ)?
Thanks for your time.
Edit. To provide the example data sets.
What I required is
The individual tables are
After the first Merge operation I have the following
Thanks again.
The table has repeating primary keys after the Merge because no primary-key was defined. So either specify the PK or try this method here which i've written from scratch(so it's not really tested):
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)
{
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;
}
You can call it in this way:
var tables = new[] { tblA, tblB, tblC };
DataTable TblUnion = tables.MergeAll("c1");
Used this sample data:
var tblA = new DataTable();
tblA.Columns.Add("c1", typeof(int));
tblA.Columns.Add("c2", typeof(int));
tblA.Columns.Add("c3", typeof(string));
tblA.Columns.Add("c4", typeof(char));
var tblB = new DataTable();
tblB.Columns.Add("c1", typeof(int));
tblB.Columns.Add("c5", typeof(int));
tblB.Columns.Add("c6", typeof(string));
tblB.Columns.Add("c7", typeof(char));
var tblC = new DataTable();
tblC.Columns.Add("c1", typeof(int));
tblC.Columns.Add("c8", typeof(int));
tblC.Columns.Add("c9", typeof(string));
tblC.Columns.Add("c10", typeof(char));
tblA.Rows.Add(1, 8500, "abc", 'A');
tblA.Rows.Add(2, 950, "cde", 'B');
tblA.Rows.Add(3, 150, "efg", 'C');
tblA.Rows.Add(4, 850, "ghi", 'D');
tblA.Rows.Add(5, 50, "ijk", 'E');
tblB.Rows.Add(1, 7500, "klm", 'F');
tblB.Rows.Add(2, 900, "mno", 'G');
tblB.Rows.Add(3, 150, "opq", 'H');
tblB.Rows.Add(4, 850, "qrs", 'I');
tblB.Rows.Add(5, 50, "stu", 'J');
tblC.Rows.Add(1, 7500, "uvw", 'K');
tblC.Rows.Add(2, 900, "wxy", 'L');
tblC.Rows.Add(3, 150, "yza", 'M');
tblC.Rows.Add(4, 850, "ABC", 'N');
tblC.Rows.Add(5, 50, "CDE", 'O');
After DataTable.Merge in MergeAll:
After some modifications to join the rows in MergeAll:
Update
Since this question arose in one of the comments, if the only relation between two tables is the index of a DataRow in the table and you want to merge both tables according to the index:
public static DataTable MergeTablesByIndex(DataTable t1, DataTable t2)
{
if (t1 == null || t2 == null) throw new ArgumentNullException("t1 or t2", "Both tables must not be null");
DataTable t3 = t1.Clone(); // first add columns from table1
foreach (DataColumn col in t2.Columns)
{
string newColumnName = col.ColumnName;
int colNum = 1;
while (t3.Columns.Contains(newColumnName))
{
newColumnName = string.Format("{0}_{1}", col.ColumnName, ++colNum);
}
t3.Columns.Add(newColumnName, col.DataType);
}
var mergedRows = t1.AsEnumerable().Zip(t2.AsEnumerable(),
(r1, r2) => r1.ItemArray.Concat(r2.ItemArray).ToArray());
foreach (object[] rowFields in mergedRows)
t3.Rows.Add(rowFields);
return t3;
}
Sample:
var dt1 = new DataTable();
dt1.Columns.Add("ID", typeof(int));
dt1.Columns.Add("Name", typeof(string));
dt1.Rows.Add(1, "Jon");
var dt2 = new DataTable();
dt2.Columns.Add("Country", typeof(string));
dt2.Rows.Add("US");
var dtMerged = MergeTablesByIndex(dt1, dt2);
The result table contains three columns ID,Name,Country and a single row: 1 Jon US
I have a datatable in windows form and I want to add a new row to the table. I always receive the error
Table doesn't have a primary key.
The question is that I don't want a primary key at all. It may have duplicated "ID" in the table.
My code:
using (DataTable dt = new DataTable())
{
dt.Columns.Add("EntryDE", typeof(string));
dt.Columns.Add("Descr", typeof(string));
dt.Columns.Add("Id", typeof(int));
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
if (check.Value != null)
{
if ((bool)check.Value)
{
//this row has a checkBox set to true (tick is added)
//add this row to dataTable ...
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
DataRow dr = dt.NewRow();
dr["EntryDE"] = myRow["ID"].ToString();
dr["Descr"] = myRow["EntryName"];
dr["Id"] = Id;
dt.Rows.Add(dr);
}
}
}
Thanks for advice.
The error isn't coming from the DataTable because this works:
using (DataTable dt = new DataTable())
{
dt.Columns.Add("EntryDE", typeof(string));
dt.Columns.Add("Descr", typeof(string));
dt.Columns.Add("Id", typeof(int));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["EntryDE"] = "abc";
dr["Descr"] = "xyz";
dr["Id"] = 1;
dt.Rows.Add(dr);
}
}
You've got some other problem.
You probably do want a primary key, as without it you'll be unable to update or delete rows later on.
If the ID field is going to contain duplicates, then you should add another column, call it primary_key or whatever you like, and use that column as PK.