I have two DataTables in an ASP.NET application written in C# (dtA and dtB). The both get filled from textboxes from user input. Each of them will only have one datarow at a time. For example, dtA can have the following values [ABC, DEF] where the column names are first name and last name, and dtB can having the following values [50, 100, 95] where the column names are grades. I need to know how to combine the two of these tables into a new datatable called dtC this way I can return dtC.
Something like:
var dtC = new DataTable("CombinationOfBoth");
dtC.Columns.Add("Firstname",typeof(string));
dtC.Columns.Add("Lastname", typeof (string));
dtC.Columns.Add("Grade1", typeof (int));
dtC.Columns.Add("Grade2", typeof(int));
dtC.Columns.Add("Grade3", typeof(int));
dtC.Merge(dtA,false,MissingSchemaAction.Ignore);
dtC.Merge(dtB, false, MissingSchemaAction.Ignore);
Please note that the dtA should have same column names as "Firstname", and "LastName", also dtB should have "Grade1","Grade2","Grade3"
or you can change the name of the columns on the dtC to be exactly same as the ones that are on dtA and dtB
Related
so my problem is that I want to display a table inside another table in WPF.
I use a DataTable in order to display some data and there is one column, in which I need to display another DataTable. I set AutoGenerateColumns="True". For a little testing, this is what I wrote (well, it works as expected):
var curDataTable = new DataTable();
curDataTable.Columns.Add("name" , typeof(string));
curDataTable.Columns.Add("number", typeof(int));
DataRow curRowData = curDataTable.NewRow();
curRowData[0] = "jones";
curRowData[1] = 90;
curDataTable.Rows.Add(curRowData);
Now, let's say I already have a filled DataTable _dataTable. I now want to display this _dataTable in my second column. This is what i would expect to work, but what does not work:
var curDataTable = new DataTable();
curDataTable.Columns.Add("name" , typeof(string));
curDataTable.Columns.Add("table", typeof(DataTable));
DataRow curRowData = curDataTable.NewRow();
curRowData[0] = "jones";
curRowData[1] = _dataTable;
curDataTable.Rows.Add(curRowData);
Has anyone an idea how to fix this?
Using nested DataTables won't work.
What you should do is to replace the outer DataTable with a custom type and bind to a custom collection property of this type in a DataGridTemplateColumn.
I don't think that nested DataTables are possible...
What you can do is create new column in '_datatable' with the name of "name"
_datatable.Columns.Add("name" , typeof(string)).SetOrdinal(0);
Using SetOrdinal(0) you can make "name" column first column. Now you maybe able to add new columns to it.
I hope it helps
I need to set a column's value in a datatable as the sum of other two columns in a datatable.I tried expression column and is working fine . But the problem is that the value of expression column is not getting updated always.Suppose A,B are two columns in the datatable and the sum of these two columns should be shown in Column 'C'.When when the value of B is changed then the value of columns 'C' is also getting changed.But not when column 'A' is changed
DataTable dt1 = new DataTable();
dt1.Columns.Add("A", typeof(decimal));
dt1.Columns.Add("B", typeof(decimal));
dt1.Columns.Add("C", typeof(decimal), "A+B");
myGridView.Datasource = dt1;
This is an educated guess without seeing your code but it seems as though if you are using Compute then you quite possibly shouldn't use an expression column for this, rather you should use a DataColumn... Taken from here
"If you must perform an operation on two or more columns, you should create a DataColumn, set its Expression property to an appropriate expression, and use an aggregate expression on the resulting column. In that case, given a DataColumn with the name "total", and the Expression property set to this: "Quantity * UnitPrice"
Should go like this:
DataTable dt1 = new DataTable();
dt1.Columns.Add("A", typeof(decimal));
dt1.Columns.Add("B", typeof(decimal));
dt1.Columns.Add("C", typeof(decimal));
foreach (DataRow dr in dt1.Rows)
{
dr[2] = Convert.ToDecimal(dr[0]) + Convert.ToDecimal(dr[1]);
}
myGridView.Datasource = dt1;
How can I copy 1 data column from 1 data table to a new datatable. When I try to do it, I get the error Column 'XXX' already belongs to another DataTable.?
dataColumn = datatable1.Columns[1];
datatable2 = new DataTable();
datatable2.Columns.Add(dataColumn);
Thanks in Advance
You cannot copy DataColumns. What you'll need to do is create a new DataColumn in the new datatable with the same data type as in the old datatable's column, and then you need to run a FOR loop to bring in all the data from the old datatable to the new datatable.
See the following code. This assumes that the datatables have exactly the same number of rows.
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt2.Columns.Add("ColumnA", dt1.Columns["ColumnA"].DataType);
for (int i = 0; i < dt1.Rows.Count; i++)
{
dt2.Rows[i]["ColumnA"] = dt1.Rows[i]["ColumnA"];
}
Also, If the data you are copying are reference types and not value types you might want to see if a .Clone() method is available for the type, or make one yourself. Just doing 'this = that' in the FOR loop will not work on reference types.
You cannot copy a DataColumn. (DataColumns are very tightly coupled with their tables)
Instead, you can add a new column with the same name and datatype.
You might be looking for DataTable.Clone(), which will create a structual copy of an entire table. (With the same schema, but no data)
Just a thought, are your DataTables both in the same DataSet?
If so, you can create a named DataRelation between the columns of two tables (think foreign key).
Then you can add a Calculated DataColumn to your table that has its Expression property set to "Child(RelationName).ColumnName" or "Parent(RelationName).ColumnName" depending on the direction of the relationship.
This will give you the same effect as copying the column, but I believe it only evaluates it lazily. So maybe it will give you what you need.
There is an example here of how this works. The example uses the Sum aggregate function, but you just need to reference the column name and it will duplicate it in your DataTable
myDataSet.Relations.Add(
"Orders2OrderLines",
myDataSet.Tables["Orders"].Columns["OrderID"],
myDataSet.Tables["OrderLines"].Columns["OrderID"]);
ordersTable.Columns.Add("OrderTotal", typeof(decimal), "Sum(Child(Orders2OrderLines).ExtendedPrice)");
HTH
The problem is caused by the c# can not reuse the object instance created and uses it on multiples DataTables. For this it is necessary to create a new object DataCollumn for each loop iteration.
foreach (DataTable table in DATASET.Tables)
{
DataColumn yourDataCollumn = new DataColumn("Name of DataCollumn", typeof(Your data type));
// your logic here
}
Hope it's help...
I used the below to merge two tables using mostly LINQ and only looping through the rows at the end. I wouldn't call it pretty but it does work. Using the join to prevent some of the assumptions listed above.
DataTable tableOne = getTableOne();
DataTable tableTwo = getTableTwo();
var oneColumns = tableOne.Columns.Cast<DataColumn>()
.Select(p => new Column(p.ColumnName, DataType))
.ToArray();
var twoColumns = tableTwo.Columns.Cast<DataColumn>()
.Select(p => new DataColumn(p.ColumnName, p.DataType))
.ToArray();
var matches = (from a in tableOne.AsEnumerable()
join b in tableTwo.AsEnumerable() on a["column_name"] equals b["column_name"]
select a.ItemArray.Concat(b.ItemArray)).ToArray();
DataTable merged = new DataTable();
merged.Columns.AddRange(oneColumns);
merged.Columns.AddRange(twoColumns);
foreach (var m in matches) { merged.Rows.Add(m.ToArray()); }
No looping required , Refer this , Hope this should solve your problem...
DataTable dt = new DataTable();
//fill the dt here
DataTable dt2 = new DataTable();
string[] strCols = {"Column Name to copy"};
dt2 = dt.DefaultView.ToTable("newTableName", false, strCols);
Is it possible to use a wildcard in a DataTable.Select(filter) call if the data type of the column is an Int32?
DataTable table = new DataTable();
DataColumn column = new DataColumn("Name", typeof(String));
DataColumn column = new DataColumn("Age", typeof(Int32));
table.Select("Name like 'b%'); // Works
table.Select("Age like '3%'); // Does NOT work.
This actually works in MySQL, so I just wondered if its possible in a DataTable.
You should be able to convert to a string and then use wildcards:
table.Select("Convert(Age, 'System.String') like '3%');
See http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx for valid expressions.
Yes but use an asterisk instead of a percentage sign for the wildcard
The syntax for the data table select statement is the same as for a data column expression - see http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx for all the supported syntax options
In C# & .NET, can one create a DataView that includes only a proper subset of the DataColumns of a given DataTable?
In terms of relational algebra, one assigns a RowFilter in order to perform a "selection" operation (σ). How would one perform a "projection" operation (π)?
You can't do that, but you can create a copy of the table with only the columns you want :
DataView view = new DataView(table);
DataTable table2 = view.ToTable(false, "FirstColumn", "SecondColumn", "ThirdColumn");
Optionally you can return rows that have distinct values for the selected columns :
DataView view = new DataView(table);
DataTable table2 = view.ToTable(true, "FirstColumn", "SecondColumn", "ThirdColumn");
Well I can't see any reason for "wanting" to do that... Remember, a DataView is just a list of pointers to the rows in the original table, and there is obviously no way to remove columns from the the original table... at least not without affecting every other function utilizing that table... Just only use the columns you want...
create dataview as a swap from one table to other table, and use the dtswap as datasource.
DataView dw = new DataView(dtfee);
DataTable dtswap = new DataTable();
dtswap = dw.ToTable(true,"Fees", "FeeAmount", "Year", "CollectorName", "Month");
DataSet and its associated types have no ability to perform relational operations.