I have two DataTable, dt1, dt2. I write a class to compare two DataTable and get rows different.
table dt1:
Col 1 Col 2 Col 3
A 8 #
B 21 ()
table dt2:
Col 1 Col 2 Col 3
A 8 ^%^%^%
C 827 _++)
It show line different like:
B 21 ()
I post my function to compare:
public static DataTable CompareDataTables(DataTable first, DataTable second)
{
first.TableName = "FirstTable";
second.TableName = "SecondTable";
//Create Empty Table
DataTable table = new DataTable("Difference");
try
{
//Must use a Dataset to make use of a DataRelation object
using (DataSet ds = new DataSet())
{
//Add tables
ds.Tables.AddRange(new DataTable[] { first.Copy(), second.Copy() });
//Get Columns for DataRelation
DataColumn[] firstcolumns = new DataColumn[ds.Tables[0].Columns.Count];
for (int i = 0; i < 2; i++)
{
firstcolumns[i] = ds.Tables[0].Columns[i];
}
DataColumn[] secondcolumns = new DataColumn[ds.Tables[1].Columns.Count];
for (int i = 0; i < 2; i++)
{
secondcolumns[i] = ds.Tables[1].Columns[i];
}
//Create DataRelation
DataRelation r = new DataRelation(string.Empty, firstcolumns, secondcolumns, false);
ds.Relations.Add(r);
//Create columns for return table
for (int i = 0; i < first.Columns.Count; i++)
{
table.Columns.Add(first.Columns[i].ColumnName, first.Columns[i].DataType);
}
//If First Row not in Second, Add to return table.
table.BeginLoadData();
foreach (DataRow parentrow in ds.Tables[0].Rows)
{
DataRow[] childrows = parentrow.GetChildRows(r);
if (childrows == null || childrows.Length == 0)
table.LoadDataRow(parentrow.ItemArray, true);
}
table.EndLoadData();
}
}
catch (Exception ex) { }
return table;
}
tableDifferent will contain all rows different.
DataTable tableDifferent;
tableDifferent = CompareDataTables(dt1, dt2);
I tried with code:
var rowsToDelete = from r1 in dt1.AsEnumerable()
join r2 in tableDifferent.AsEnumerable()
on r1.Field<String>("StudentID") equals r2.Field<String>("StudentID")
select r1;
Now, I want to remove all rows in dt1 and dt2, only keep all rows in tableDifferent.
Have any method to do this?
If the only problem left is to clear the other data tables, you can use the DataTable.Clear Method ()
All rows in all tables are removed. An exception is generated if the
table has any enforced child relations that would cause child rows to
be orphaned.
If the DataSet is bound to an XmlDataDocument, calling DataSet.Clear
or DataTable.Clear raises the NotSupportedException. To avoid this
situation, traverse each table, removing each row one at a time. When
you use NewRow to create new rows, the rows must be processed before
you call Clear
In your case:
dt1.Clear();
dt2.Clear();
Related
I tried this solution below:
This Row already belongs to another table error when trying to add rows?
I have a Datatable that contains 597 Columns and 20 Rows and are trying to export the data to excel. However, Excel has a maximum column count 256 and so I need to divide the source data into 3 datatables to make the export work.
Below is the code I have written.
var dtmasterdata = data.Tables[name];
for (int j = 1; j < datatableNumberCount; j++)
{
DataTable dt2 = new DataTable();
dt2.TableName = "Master_" + j;
dt2 = dtmasterdata.Copy();
foreach (DataColumn col in dtmasterdata.Columns)
{
DataColumn dtcol = new DataColumn();
dtcol = col;
dt2.Columns.Add(dtcol.ColumnName, dtcol.DataType);
}
for (int k = 0; k < dtmasterdata.Rows.Count; k++)
{
DataRow dr = dt2.NewRow();
dr = dtmasterdata.Rows[k];
dt2.ImportRow(dtmasterdata.Rows[k]);
//dt2.Rows.Add(dr.ItemArray);
}
After that I need to delete few columns like below and I want to create 3 datatables
foreach (DataColumn col in dtmasterdata.Columns)
{
if (j == 1)
{
// condition 1
if (col.Ordinal >= 255)
{
dt2.Columns.RemoveAt(col.Ordinal);
}
}
if (j == 2)
{
// condition 2.
if (col.Ordinal < 255 || col.Ordinal >= 510)
{
dt2.Columns.RemoveAt(col.Ordinal);
}
}
if (j == 3)
{
// condition 3.
if (col.Ordinal <= 510 || col.Ordinal >= 765)
{
dt2.Columns.Add(col);
}
}
}
int worksheetNumber = 1;
string worksheetNameWithNumber = "Master Data";
if (worksheetNumber > 1)
worksheetNameWithNumber = String.Format("{0}_{1}", ws1, worksheetNumber.ToString());
Infragistics.Excel.Worksheet worksheet = wb.Worksheets.Add(worksheetNameWithNumber);
Infragistics.WebUI.UltraWebGrid.UltraWebGrid masterData1 = new Infragistics.WebUI.UltraWebGrid.UltraWebGrid("masterDataGrid");
masterData1.Browser = Infragistics.WebUI.UltraWebGrid.BrowserLevel.UpLevel;
masterData1.DataSource = dt2;
masterData1.DataMember = "Master_" + j;
masterData1.DisplayLayout.HeaderStyleDefault.Font.Bold = true;
masterData1.DisplayLayout.HeaderStyleDefault.Font.Name = "Arial";
masterData1.DisplayLayout.HeaderStyleDefault.Font.Size = FontUnit.Parse("10px");
masterData1.DisplayLayout.HeaderStyleDefault.BackColor = System.Drawing.Color.LightGray;
masterData1.DisplayLayout.RowStyleDefault.Font.Name = "Arial";
masterData1.DisplayLayout.RowStyleDefault.Font.Size = FontUnit.Parse("10px");
Infragistics.WebUI.UltraWebGrid.UltraGridBand masterBand1 = new Infragistics.WebUI.UltraWebGrid.UltraGridBand();
masterData1.Bands.Add(masterBand1);
dgResults.Controls.Add(masterData1);
masterData1.DataBind();
wb.ActiveWorksheet = worksheet;
this.ugWebGridExporter.Export(masterData1, worksheet);
worksheetNumber++;
Your error is because you are trying to add a column to a datatable that already belongs to your source datatable.
dt2.Columns.Add(col);
You can't just iterate through the columns of a datatable and add them to another.
I've a solution to this, which involves cloning the source data and removing what you don't need.
1st, make 3 clones of the datatables you need. Below is an example with me creating my own source table with 596 columns. Notice that clone only takes the data table structure, no data!
var source597ColsTable = new DataTable("Source");
for (var i = 0; i <= 596; i++)
{
source597ColsTable.Columns.Add(new DataColumn("Column" + i , typeof(string)));
}
DataRow newRow = source597ColsTable.NewRow();
source597ColsTable.Rows.Add(newRow);
var cols0To199Table = source597ColsTable.Clone();
var cols200To399Table = source597ColsTable.Clone();
var cols400To596Table = source597ColsTable.Clone();
Next copy all the rows from the source table into the clones. The below is a simple function to do so.
private DataTable CopyRowsFromSource(DataTable sourceTable, DataTable destinationTable)
{
foreach (DataRow row in sourceTable.Rows)
{
destinationTable.Rows.Add(row.ItemArray);
}
return destinationTable;
}
Then call this function for each of your tables.
cols0To199Table = CopyRowsFromSource(source597ColsTable, cols0To199Table);
cols200To399Table = CopyRowsFromSource(source597ColsTable, cols200To399Table);
cols400To596Table = CopyRowsFromSource(source597ColsTable, cols400To596Table);
Finally, remove all the columns from the datatables to give you your split.
private DataTable RemoveColumns(DataTable table, int startCol, int endCol)
{
var colsToRemove = new List<DataColumn>();
for (var colCount = startCol; colCount <= endCol; colCount++)
{
colsToRemove.Add(table.Columns[colCount]);
}
foreach (DataColumn col in colsToRemove)
{
table.Columns.Remove(col);
}
return table;
}
Then call.. again for each cloned table.
cols0To199Table = RemoveColumns(cols0To199Table, 200, 596);
cols200To399Table = RemoveColumns(cols200To399Table, 0, 199);
cols200To399Table = RemoveColumns(cols200To399Table, 200, 396);
cols400To596Table = RemoveColumns(cols400To596Table, 0, 399);
After running this, you will have 3 datatables, columns 0-199, 200-399 and 400-596.
Hope that helps.
I am not sure to have really understood all of your code, but to copy a subset of columns to another datatable there is a very simple method in the DataView class named ToTable where you can list the columns you want in the new table. As added bonus, this method copies also the data in the 20 rows of your original table.
So the only difficult is to list these columns to the method.
You can proceed in this way using linq over the DataColumn collection
string[] firstCols = dtmasterdata.Columns
.Cast<DataColumn>()
.Take(255)
.Select(x => x.ColumnName).ToArray();
string[] secondCols = dtmasterdata.Columns
.Cast<DataColumn>()
.Skip(255)
.Take(255)
.Select(x => x.ColumnName).ToArray();
string[] thirdCols = dtmasterdata.Columns
.Cast<DataColumn>()
.Skip(510)
.Select(x => x.ColumnName).ToArray();
DataTable t1 = dtmasterdata.DefaultView.ToTable("Master_1", false, firstCols);
DataTable t2 = dtmasterdata.DefaultView.ToTable("Master_2", false, secondCols);
DataTable t3 = dtmasterdata.DefaultView.ToTable("Master_3", false, thirdCols);
Assume that I have a list like PL = { P1, 0, 10, P2, 5, 20 } and I need to convert it a datatable like
ProcessName ArrivalTime CpuTime
P1 0 10
P2 5 20
The number of process (row count) is dynamic. And I have tried sth like this:
protected DataTable CreateDataTable()
{
int j = 0;
List<string> PL = CreateProcessList();
DataTable DT = new DataTable();
for (int i = 0; i < PL.Count - 2; i += 3)
{
DataRow ProcessRow = DT.NewRow();
DT.Rows[j][0] = PL[i].ToString();
DT.Rows[j][1] = Convert.ToInt32(PL[i + 1]);
DT.Rows[j][2] = Convert.ToInt32(PL[i + 2]);
j++;
}
DT.Columns.Add("Header", typeof(string));
DT.Columns[0].ColumnName = "ProcessName";
DT.Columns[1].ColumnName = "ArrivalTime";
DT.Columns[2].ColumnName = "CpuTime";
return DT;
}
It does not work (says that there is no row at position 0). Thanks for any idea.
Working Code After Editions:
protected DataTable CreateDataTable()
{
List<string> PL = CreateProcessList();
DataTable DT = new DataTable();
DT.Columns.Add("ProcessName", typeof(string));
DT.Columns.Add("ArrivalTime", typeof(int));
DT.Columns.Add("CpuTime", typeof(int));
for (int i = 0; i < PL.Count - 2; i += 3)
{
DataRow ProcessRow = DT.NewRow();
ProcessRow[0] = PL[i].ToString();
ProcessRow[1] = Convert.ToInt32(PL[i + 1]);
ProcessRow[2] = Convert.ToInt32(PL[i + 2]);
DT.Rows.Add(ProcessRow);
}
return DT;
}
To create a datatable in the way you have described, you need to follow a different way.
Create a datatable object
Add Columns to the datatable object by using the Add() method
use the datatable objects NewRow() method to get a DataRow object with the same schema as your datatable
populate the columns of this DataRow with the desired values
Add this DataRow to the Rows collection of your datatable object by using the Add() method
repeat step 3 to 6 until your list reaches the end.
On the second iteration of your for loop i is 3, so you're getting the 4th row of your table (at this point in time your table has 2 row). You're then getting the 4th, 5th, and 6th columns of that row (your table has 0 columns, as you haven't added any columns yet) to set their value. The corresponding index out of range errors should be telling you exactly what's wrong here.
Don't access the i-th row from the table. Just use ProcessRow to access the row; it's right there in a variable for you. Don't access the i-th column, access the 1st, 2nd, and 3rd (and add the columns before you try to populate them.
Something like this should work. Keep adding to curRow until the current iteration starts with 'P'. When it does start with 'P', add the currentRow to the data table and start a new row.
DataTable dataTable;
DataRow curRow;
... add columns to dataTable
for (var i = 0; i < PL.Count; i++) {
if (PL[i].ToString().StartsWith("P")) {
if (curRow != null)
dataTable.Rows.Add(curRow);
curRow = dataTable.NewRow();
}
... add PL[i] to curRow
}
There's a few little issues with this but they can be fixed pretty easily. A check to make sure curRow is not null before adding to data table... that sorta thing.
I need to get the count of all cells in a DataTable, how can i do this ? I need this to verify that my 2 tables has the same cell amount before inserting data into a database.
Try this:
DataTable dt1 = new DataTable(), dt2 = new DataTable();
// define and populate your tables
int count1 = dt1.Columns.Count * dt1.Rows.Count;
int count2 = dt2.Columns.Count * dt2.Rows.Count;
bool verified = count1 == count2;
DataTable tbl = new DataTable();
foreach (DataRow row in tbl.Rows)
{
foreach (DataColumn col in tbl.Columns)
{
object cellData = row[col];
}
}
I have a datatable and would like to know if its possible for me to select certain columns and input the data on a table. the columns are set out as below
|col1 |col2 |col3|col4 |col5 |col6|col7 |col8 |col9 |col10 |col11 |
I want to select column col1, col2 col6, col7,col3. and dispay the data in a gridview of the rows within the datatable.. currently the code that i am using is below and onmly selects certain data. I am not selecting the data from sql its data being selected from another excel which is stored in a datatable.. but i am in need of the other columns in another area as well.. this data is being written into a table in word
for (int i = 1; i < table.Rows.Count; i++)
{
for (int j = 0; j < table.Columns.Count; j++)
{
if (j == 0)
{
val = filteredData.Rows[row][col].ToString();
}
else
{
val = filteredData.Rows[row][col].ToString();
if (val == "-" || val == "")
{
val = filteredData.Rows[row][col].ToString();
}
else
{
val = Convert.ToString(Math.Round(Convert.ToDouble(filteredData.Rows[row][col]), MidpointRounding.AwayFromZero));
}
}
table[j, i].TextFrame.Text = val;
col++;
}
Also we can try like this,
string[] selectedColumns = new[] { "Column1","Column2"};
DataTable dt= new DataView(fromDataTable).ToTable(false, selectedColumns);
First store the table in a view, then select columns from that view into a new table.
// Create a table with abitrary columns for use with the example
System.Data.DataTable table = new System.Data.DataTable();
for (int i = 1; i <= 11; i++)
table.Columns.Add("col" + i.ToString());
// Load the table with contrived data
for (int i = 0; i < 100; i++)
{
System.Data.DataRow row = table.NewRow();
for (int j = 0; j < 11; j++)
row[j] = i.ToString() + ", " + j.ToString();
table.Rows.Add(row);
}
// Create the DataView of the DataTable
System.Data.DataView view = new System.Data.DataView(table);
// Create a new DataTable from the DataView with just the columns desired - and in the order desired
System.Data.DataTable selected = view.ToTable("Selected", false, "col1", "col2", "col6", "col7", "col3");
Used the sample data to test this method I found:
Create ADO.NET DataView showing only selected Columns
The question I would ask is, why are you including the extra columns in your DataTable if they aren't required?
Maybe you should modify your SQL select statement so that it is looking at the specific criteria you are looking for as you are populating your DataTable.
You could also use LINQ to query your DataTable as Enumerable and create a List Object that represents only certain columns.
Other than that, hide the DataGridView Columns that you don't require.
Here's working example with anonymous output record, if you have any questions place a comment below:
public partial class Form1 : Form
{
DataTable table;
public Form1()
{
InitializeComponent();
#region TestData
table = new DataTable();
table.Clear();
for (int i = 1; i < 12; ++i)
table.Columns.Add("Col" + i);
for (int rowIndex = 0; rowIndex < 5; ++rowIndex)
{
DataRow row = table.NewRow();
for (int i = 0; i < table.Columns.Count; ++i)
row[i] = String.Format("row:{0},col:{1}", rowIndex, i);
table.Rows.Add(row);
}
#endregion
bind();
}
public void bind()
{
var filtered = from t in table.AsEnumerable()
select new
{
col1 = t.Field<string>(0),//column of index 0 = "Col1"
col2 = t.Field<string>(1),//column of index 1 = "Col2"
col3 = t.Field<string>(5),//column of index 5 = "Col6"
col4 = t.Field<string>(6),//column of index 6 = "Col7"
col5 = t.Field<string>(4),//column of index 4 = "Col3"
};
filteredData.AutoGenerateColumns = true;
filteredData.DataSource = filtered.ToList();
}
}
You can create a method that looks like this:
public static DataTable SelectedColumns(DataTable RecordDT_, string col1, string col2)
{
DataTable TempTable = RecordDT_;
System.Data.DataView view = new System.Data.DataView(TempTable);
System.Data.DataTable selected = view.ToTable("Selected", false, col1, col2);
return selected;
}
You can return as many columns as possible.. just add the columns as call parameters as shown below:
public DataTable SelectedColumns(DataTable RecordDT_, string col1, string col2,string col3,...)
and also add the parameters to this line:
System.Data.DataTable selected = view.ToTable("Selected", false,col1, col2,col3,...);
Then simply implement the function as:
DataTable myselectedColumnTable=SelectedColumns(OriginalTable,"Col1","Col2",...);
Thanks...
DataView dv = new DataView(Your DataTable);
DataTable dt = dv.ToTable(true, "Your Specific Column Name");
The dt contains only selected column values.
I need to delete columns from datatable where all rows have 0 for value. Or in other words, where sum is 0.
1 2 5 99.9 442.25 221 0
1 2 77.7 889 898 55 0
9 0 66 42 55 0 0
In this example, last column should be removed.
How to do this?
DataTable dt;
int dataWidth = 5; //use a loop or something to determine how many columns will have data
bool[] emptyCols = new bool[datawidth]; //initialize all values to true
foreach(Row r in dt)
{
for(int i = 0; i < dataWidth; i++)
{
if(r[i].Contents != 0))
emptyCols[i] = false;
}
}
for(int i = 0; i < emptyCols.Length; i++)
{
if(emptyCols[i])
dt.Columns.RemoveAt(i);
}
I haven't tested that, but I have done similar stuff with excel columns. The basic logic is there, I don't know if all of my increments or row numbering is exactly correct. I believe most of the functions I used are available as well.
First:
protected Boolean IsColumnZero(DataTable dt, string columnName)
{
foreach (DataRow row in dt.Rows)
if ((int)row[columnName] != 0) return false;
return true;
}
and then You can:
//create table
DataTable table = new DataTable();
table.Columns.Add("caliber", typeof(int));
table.Columns.Add("barrel", typeof(int));
table.Rows.Add(762, 0);
table.Rows.Add(556, 0);
table.Rows.Add(900, 0);
//delete zero value columns
List<string> columnsToDelete = new List<string>();
foreach (DataColumn column in table.Columns)
if (IsColumnZero(table, column.ColumnName))
columnsToDelete.Add(column.ColumnName);
foreach (string ctd in columnsToDelete) table.Columns.Remove(ctd);
//show results
GridView1.DataSource = table;
GridView1.DataBind();