Below is my current format for exporting the results of Datatable and write into the excel
Would you please How can I change the foreach into forloop and check the name of the Data Column ?
For instance, I have got a data Table as selecting all details from customers , such as ID, name , product , price
I wan to access of the current position of looping data column is on the product or not
foreach (DataRow r in table.Rows)
{
currentCol = 1;
foreach (DataColumn c in table.Columns)
{
excelDoc.setCell(1, 1, currentRow, currentCol, r[currentCol - 1].ToString());
currentCol++;
}
currentRow++;
}
Something like:
DataRow row;
for(int currentRow=0; currentRow < table.Rows.Count; currentRow++)
{
row = table.Rows[currentRow];
for (int currentCol = 0; currentCol < table.Columns.Count; currentCol++ )
{
if (table.Columns[currentCol].ColumnName == "product")
{
excelDoc.setCell(1, 1, currentRow, currentCol, row[currentCol - 1].ToString());
}
}
}
Related
I have a data table named dt which is a SQL table converted.
All the data was sent from the server so I had to encrypt it. Now I need to decrypt every cell.
I wanted to do it like this:
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
row[i] = r.Decrypt(row[i].ToString());
}
}
Decrypt works fine but row[i] = r.Decrypt(row[i].ToString()); give me the number of the column instead of its content, how do I get the content?
to make the post make more sense.
i want row[i] = r.Decrypt(a string representing the value of row[i]);
This is the correct way to interact with a datatable
foreach(DataRow row in dt .Rows)
{
foreach(DataColumn column in dt .Columns)
{
Console.WriteLine(row[column]);
}
}
In your case you are literally passing an int to your row, because You are using count property.
** Edit
This is in case Your datarow contains an object.
DataTable dt = new DataTable();
dt.Columns.Add("Column 1");
dt.Columns.Add("Column 2");
DataRow dataRow = dt.NewRow();
dataRow.ItemArray = new object[]{"line 1 column 1", "line 1 column 2"
};
dt.Rows.Add(dataRow);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
var text = row[column.ToString()];
Console.WriteLine(text);
}
}
Result
If none of this work, We would have to see how Your data row is created.
As you mentioned that the other logic (Decrypt) is working fine and you just need to fetch the column content/values then you can try something like:
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
// you can insert your logic to use column value row[column] here
Console.WriteLine(row[column]);
}
}
for (int i = 0; i < dt.Rows.Count ; i++)
{
for (int j = 1; j < dt.Columns.Count ; j++)
{
string cellValue = dt.Rows[i][j].ToString();
dt.Rows[i][j] = r.Decrypt(cellValue);
}
}
I have a Table with the below column names.
1) HSN Code
2) Item Code
3) Unit Cost
4) Total
If the "HSN Code" field value is empty in any row I want to merge the remaining fields data to the Above row. How can I do that in C#?
Try following :
DataTable dt = new DataTable();
int numberColumns = dt.Columns.Count;
foreach(DataRow row in dt.AsEnumerable())
{
if(row["HSN Code"] == string.Empty)
{
row["HSN Code"] = string.Join(",", row.ItemArray.Skip(1).Select(x => x.ToString()));
for (int i = 1; i < numberColumns; i++)
{
row[i] = DBNull.Value;
}
}
}
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 a data table that I am creating from an excel file. This works fine. But, I recently had a spec change and need to substitute a value in column 0 of the table. I am having trouble getting my IF statement to fire. It is probably something simple I am overlooking.
Basically, if it is the first column in the row, I want to put a specific value I pulled elsewhere (I am pulling the color of the cell and putting in the RGB value). If it is any other column in the row, continue to read in the data as usual.
Please see the below code:
DataTable tbl = new DataTable();
foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
{
tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
//var cellColor = ws.Cells[firstRowCell].Style.Fill.BackgroundColor.LookupColor();
}
//var startRow = hasHeader ? 2 : 1;
for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
{
var currentRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
DataRow row = tbl.Rows.Add();
for (var i = 0; i < tbl.Columns.Count; i++)
{
//This is my problem. I am not seeing what needs to be in here.
if (tbl.Columns.????????)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}
}
You're in a for loop that starts at your first column to the last
for (var i = 0; i < tbl.Columns.Count; i++)
So the value you can use to know at what column you're at would be the i, you could have something like this
for (var i = 0; i < tbl.Columns.Count; i++)
{
//If you're at the first column
if (i == 0)
{
row[i] = colorTable[rowNum];
}
else
{
row[i] = currentRow[rowNum, i + 1].Text;
}
}
Trying to do a sum of each column in a DataTable that i have set up. The First 2 columns in the datatable contain text and the rest of the columns contain numeric values that i would like to sum.
I get a error saying there is no row at position 17.
when i scroll down the DataTable viewer you can see there is no row at 17 and cant see why the foreach is hitting this row.
I have the following code
for (int i = 2; i < DT.Columns.Count; i++)
{
foreach (DataRow DR in DT.Rows)
{
ColumnTotal = ColumnTotal + int.Parse(DR[i].ToString());
}
DT.Rows[DT.Rows.Count + 1][i] = ColumnTotal;
ColumnTotal = 0;
}
Can anyone advise me on what i am doing wrong and how i can improve on it.
Thanks
You are trying to assign in 17-th row but it does not exist. So first create it and then populate it like below:
var row = DT.NewRow();
for (int i = 2; i < DT.Columns.Count; i++)
{
ColumnTotal = 0;
foreach (DataRow DR in DT.Rows)
{
ColumnTotal = ColumnTotal + int.Parse(DR[i].ToString());
}
row[i] = ColumnTotal;
}
DT.Rows.Add(row);
You can not set the value in row like this
DT.Rows[DT.Rows.Count + 1][i] = ColumnTotal;
Because it has no 17th row, You may need to add a new row.
Do something like,
DataRow newRow = DT.NewRow();
for (int i = 2; i < DT.Columns.Count; i++)
{
foreach (DataRow DR in DT.Rows)
{
ColumnTotal = ColumnTotal + int.Parse(DR[i].ToString());
}
newRow[i] = ColumnTotal;
ColumnTotal = 0;
}
DT.Rows.Add(newRow);
simple my dear friend
Datatable Dt=new DataTable();
Dt.Columns.Add("Name");
Dt.Columns.Add("Amount");
Dt.Columns.Add("Id");
Dt.Rows.Add("Harsh","100", "1");
Dt.Rows.Add("Miller","200", "2");
now to calculate use this
object Sum = Dt.Compute(" SUM(Amount) ","");
double NetTotal = Double.Parse(Sum.ToString());