I have a DataSet with many similar tables in it. I need to union all datatables in a single table. So for this example; I need a datatable with 20 rows. How can I do that?
DataSet SampleDS = new DataSet();
DataTable SampleTbl1 = new DataTable();
SampleTbl1.Columns.Add("Product", typeof(string));
SampleTbl1.Columns.Add("Value", typeof(int));
for (int i = 0; i < 10; i++)
{
SampleTbl1.Rows.Add("Product " + i, i);
}
SampleDS.Tables.Add(SampleTbl1);
DataTable SampleTbl2 = new DataTable();
SampleTbl2.Columns.Add("Product", typeof(string));
SampleTbl2.Columns.Add("Value", typeof(int));
for (int i = 0; i < 10; i++)
{
SampleTbl2.Rows.Add("Product " + i, i*2);
}
SampleDS.Tables.Add(SampleTbl2);
You can make a new DataTable, then import rows of two tables into new table.
DataTable t = new DataTable();
t.Columns.Add("Product", typeof(string));
t.Columns.Add("Value", typeof(int));
foreach(DataTable table in SampleDS.Tables)
{
if(table != null && table.Rows.Count > 0)
{
for(int i = 0; i < table.Rows.Count; i ++)
t.ImportRow(table.Rows[i]);
}
}
HTH.
Try with LINQ
var table = SampleDS.Tables[0].AsEnumerable();
for (var ii = 1; ii < SampleDS.Tables.Count; ii++) {
table = table.Concat(SampleDS.Tables[ii].AsEnumerable());
}
Related
I want to make data entry grid with validations and item template field which consist of multiple Webform controls. like SAP and Ax Dynamics data entry grid.
Kindly help me if you anyone have this kind of sample grid.
//You can store a DataTable in the session state
DataTable table = Session["Table1"] as DataTable;
table = new DataTable();
DataColumn colid = table.Columns.Add("sno", typeof(Int32));
DataColumn l_ID = table.Columns.Add("l_ID", typeof(String));
table.PrimaryKey = new DataColumn[] { colid };
colid.ReadOnly = true;
l_ID.ReadOnly = true;
getlist();
//List<Dictionary<string, string>> list = grid.GRCol();
for (int i = 0; i < list.Count; i++)
{
DataColumn dcol = new DataColumn(list[i]["fieldid"], typeof(string));
table.Columns.Add(dcol);
}
#region rowgenrate
for (int i = 1; i <= 1; i++)
{
DataRow aRow = table.NewRow();
for (int j = 0; j < table.Columns.Count; j++)
{
if (i == 1 && j > 1)
{
string abc = table.Columns[j].ToString();
aRow["sno"] = i;
aRow["l_ID"] = "001";
aRow[abc] = "";
//table.Rows.Add(aRow);
}
else
{
if (j > 1)
{
aRow["sno"] = i;
aRow["l_ID"] = "";
aRow[j] = "";
}`enter code here`
//table.Rows.Add(aRow);
}
}
table.Rows.Add(aRow);
}
#endregion
Session["Table1"] = table;
How can I query an Excel file where the rows and columns are reversed / rotated 90 degrees?
Can it be done with a SELECT query, or do I need to recurse the cells programmatically?
It's for a .NET app, so linq or other suggestions are welcome.
Transpose a Datatable with a code like this:
private DataTable GenerateTransposedTable(DataTable inputTable)
{
DataTable outputTable = new DataTable(inputTable.TableName);
outputTable.Columns.Add(inputTable.Columns[0].ColumnName);
foreach (DataRow inRow in inputTable.Rows)
{
string newColName = inRow[0].ToString();
outputTable.Columns.Add(newColName);
}
for (int rCount = 1; rCount <= inputTable.Columns.Count - 1; rCount++)
{
DataRow newRow = outputTable.NewRow();
newRow[0] = inputTable.Columns[rCount].ColumnName;
for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
{
string colValue = inputTable.Rows[cCount][rCount].ToString();
newRow[cCount + 1] = colValue;
}
outputTable.Rows.Add(newRow);
}
return outputTable;
}
.NET does not include a method to transpose data tables. You have to make your own. This website Link has a tutorial on an example transpose method. I will copy and paste the code snippet below:
private DataTable Transpose(DataTable dt)
{
DataTable dtNew = new DataTable();
//adding columns
for(int i=0; i<=dt.Rows.Count; i++)
{
dtNew.Columns.Add(i.ToString());
}
//Changing Column Captions:
dtNew.Columns[0].ColumnName = " ";
for(int i=0; i<dt.Rows.Count; i++)
{
//For dateTime columns use like below
dtNew.Columns[i+1].ColumnName =Convert.ToDateTime(dt.Rows[i].ItemArray[0].ToString()).ToString("MM/dd/yyyy");
//Else just assign the ItermArry[0] to the columnName prooperty
}
//Adding Row Data
for(int k=1; k<dt.Columns.Count; k++)
{
DataRow r = dtNew.NewRow();
r[0] = dt.Columns[k].ToString();
for(int j=1; j<=dt.Rows.Count; j++)
r[j] = dt.Rows[j-1][k];
dtNew.Rows.Add(r);
}
return dtNew;
}
I have follwing code but it get error-Input array is longer than the number of columns in this table.
DataTable dtYear = new DataTable();
int Year1 = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
for (i = 1980; i <= Year1; i++)
{
dtYear.Rows.Add(i);
}
ddlYear.DataSource = dtYear;
ddlYear.DataBind();
Your DataTable doesn't contain any columns - so you cant add a row containing a value for a column.
Edit:
Change your code like this:
DataTable dtYear = new DataTable();
dtYear.Columns.Add("Year", typeof(int)); // add this line
int Year1 = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
for (i = 1980; i <=> Year1; i++)
{
dtYear.Rows.Add(i);
}
ddlYear.DataSource = dtYear;
ddlYear.DataBind();
Add "Year" column to the DataTable first.
dtTable.Columns.Add("Year", typeof(int));
You can use the Year property of the DateTime.
int Year1 = DateTime.Now.Year;
And also you should use a valid compare operator in the for condition. <=> is not valid in c#.
for (i = 1980; i <= Year1; i++)
{
}
You have to add the column to the DataTable first:
DataTable dtYear = new DataTable();
dtYear.Columns.Add("year",typeof(int)); // add the column
int Year1 = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
for (int i = 1980; i <= Year1; i++)
{
dtYear.Rows.Add(i);
}
ddlYear.DataSource = dtYear;
ddlYear.DataBind();
I don't think that your code will even compile, so how is it throwing an error.
use Data Columns before assigning values to row.
here is some modification in your code.
DataTable dtYear = new DataTable();
int Year1 = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
//add columns to DataTable
dtYear.Columns.Add("Years",typeof(int), null);
for (int i = 1980; i <= Year1; i++)
{
dtYear.Rows.Add(i);
}
ddlYear.DataSource = dtYear;
ddlYear.DataTextField = "Years";
ddlYear.DataBind();
hope this will help.
Is it possible to have a horizontal columns in DataGridView, with ability to bind those columns?
You dont have to Flip the DataGridView instead Flip the DataSet to bind
Try this:
public DataSet FlipDataSet(DataSet my_DataSet)
{
DataSet ds = new DataSet();
foreach (DataTable dt in my_DataSet.Tables)
{
DataTable table = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{ table.Columns.Add(Convert.ToString(i)); }
DataRow r;
for (int k = 0; k < dt.Columns.Count; k++)
{
r = table.NewRow();
r[0] = dt.Columns[k].ToString();
for (int j = 1; j <= dt.Rows.Count; j++)
{ r[j] = dt.Rows[j - 1][k]; }
table.Rows.Add(r);
}
ds.Tables.Add(table);
}
return ds;
}
For more details visit Displaying-Vertical-Rows-in-DataGrid-View
Is it possible to convert a sqlceresultset.resultview to datatable?
Not tested, but this should do what you need:
public DataTable ResultSetToDataTable(SqlCeResultSet set)
{
DataTable dt = new DataTable();
// copy columns
for (int col = 0; col < set.FieldCount; col++)
{
dt.Columns.Add(set.GetName(col), set.GetFieldType(col));
}
// copy data
while (set.Read())
{
DataRow row = dt.NewRow();
for (int col = 0; col < set.FieldCount; col++)
{
int ordinal = set.GetOrdinal(GetName(col));
row[col] = set.GetValue(ordinal);
}
dt.Rows.Add(row);
}
return dt;
}
There's no built-in way to do this (that I know of), probably because a SqlCeResultSet does not store actual data like a DataTable does.