I need to go through each and every row of a datatable - c#

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);
}
}

Related

C# Adding dynamic column and rows in Datatable

I tried to add values to DataTable columns by vertically in loops.
But output doesn't looks good as expected.
DataTables dynamictable = new DataTable()
foreach (DataColumn cl in dataTable.Columns)
{
dynamictable.Columns.Add(cl.ToString());
List<string> plainList = dataTable.AsEnumerable().Select(x => x[cl].ToString()).ToList();
for (int i=0; i< plainList.Count(); i++)
{
DataRow row = dynamictable.NewRow();
row[cl.ToString()] = plainList[i];
enryptedTable.Rows.Add(row);
}
}
Actual Output got from my code:
Expected Output:
change your code to :
DataTable dynamictable = new DataTable();
int col = 0;
foreach (DataColumn cl in dataTable.Columns)
{
dynamictable.Columns.Add(cl.ToString());
List<string> plainList = dataTable.AsEnumerable().Select(x => x[cl].ToString()).ToList();
DataRow row = null;
for (int i = 0; i < plainList.Count(); i++)
{
row = dynamictable.NewRow();
row[col] = plainList[i];
dynamictable.Rows.Add(row);
}
col++;
}
//now set dynamictable to datagridview or .....

How to merge a Line Item row to the above Row based on condition

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;
}
}
}

Sum DataTable Columns

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());

C# for loop changing format DataTable

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());
}
}
}

Convert SQLCEResultSet resultview to datatable

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.

Categories

Resources