Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach (DataRow row in dt.Rows)
        {
            lstData.Add(new Tablelist() { FileName=csv,  process = row["process"] + "", Event = row["Event"] + "", status = row["status"] + "" });
 
        }
    }
        DataTable dtoutput = new DataTable();
       var columnNames =  lstData.Select(k => k.FileName).Distinct();
       foreach (string columnName in columnNames)       
           dtoutput.Columns.Add(columnName, typeof(string));
       DataRow newrowPassCount = dtoutput.NewRow();
         foreach (string columnName in columnNames) {
            int count = lstData.Where(k=> k.FileName == columnName).Count(k => k.status == "Pass");
            newrowPassCount[columnName] = count; 
        }
         dtoutput.Rows.Add(newrowPassCount);

HI...currently the last code dtoutput.Rows.Add(newrowPassCount);
will create just one row with the (newrowpasscount) value....after second loop I need another row to be created with a different (newrowpasscount) value.. what it does now it will overwrite the previous row..I've been trying this
 foreach (DataRow row in dtoutput.Rows)
{
    dtoutput.Rows.Add(newrowPassCount);
}

but it doesn't work...
Posted
Updated 13-Jan-14 17:31pm
v2

In below code u have put FileName=csv as static value and
C#
var columnNames =  lstData.Select(k => k.FileName).Distinct();


always returns one value of "csv".
so every time it set the value to same column name. and u needs to
keep
C#
DataRow newrowPassCount = dtoutput.NewRow();
inside foreach loop

let me know if its help or not.
 
Share this answer
 
v2
Comments
acing wei 14-Jan-14 1:13am    
foreach (DataRow row in dtoutput.Rows)
{
DataRow newrowPassCount = dtoutput.NewRow();
dtoutput.Rows.Add(newrowPassCount);
}
it doesnt work.. the newrowpasscount in redline cannot be declared in the scope..
acing wei 14-Jan-14 1:21am    
i think should be my foreach loop problem.. what should I use i want this (newrowpasscount) "row Value" first looping store in first new row , second looping store in another row.. any other way?
Suk@nta 14-Jan-14 1:55am    
number of fileName always returns 1 name because in first foreach loop u are assinging same value for FileName.
so it will add one row only.
acing wei 14-Jan-14 1:58am    
ehm beside that i also try this to create a new datatable..
for (int i = 1; i < dtoutput.Rows.Count; i++)
{
var tempRow = dtoutput.Rows[i];
var temp = dtoutput.Rows[i][0];
for (int j = 1; j < dtoutput.Rows.Count; j++)
{
DataRow rows = dtoutput.Rows[j];
if (temp == rows[0].ToString())
{
dtoutput.NewRow();
dtoutput.Rows.Add(tempRow[0], tempRow[1]);

}

}
}
but it endless looping ...i use for loop to keep my first loop first value in var then create a table for that row.. but i still stuck
Suk@nta 14-Jan-14 3:27am    
var tempRow = dtoutput.Rows[i];
u can not assign a row to a simple variable. u have to assign to a datatrow variable.

use the below code snippet

DataTable dt = new DataTable();
for (int i = 1; i < dtoutput.Rows.Count; i++)
{
DataRow tempRow = dtoutput.Rows[i];
var temp = tempRow[0];
var temp1 = tempRow[1];
dt.Rows.Add(temp, temp1);

}
//bind the daatatable to grid
You need to move the dtoutput.NewRow(); to the foreach loop so that each time the new row created are added to the collection.
 
Share this answer
 
Comments
acing wei 14-Jan-14 1:10am    
gives me this error"Collection was modified; enumeration operation might not execute".
foreach (DataRow row in dtoutput.Rows)
{
dtoutput.NewRow();
dtoutput.Rows.Add(newrowPassCount);
}
it doesn't work..
acing wei 14-Jan-14 3:07am    
i also try to use for loop like this
for (int i = 1; i < dtoutput.Rows.Count; i++)
{

var tempRow = dtoutput.Rows[i];

if (i == 1)
{
dtoutput.NewRow();
dtoutput.Rows.Add(tempRow);
}
}
but doesn't seems work...
JoCodes 14-Jan-14 3:11am    
Try this for (int i = dtoutput.Rows.Count - 1; i >= 0; i--)
{
}
acing wei 14-Jan-14 4:09am    
It cannot work the way i want, i still stick with this
foreach (DataRow row in dtoutput.Rows)
{
dtoutput.NewRow();
dtoutput.Rows.Add(newrowPassCount);
}
but I keep receive error gives me this error"Collection was modified; enumeration operation might not execute". the idea is something like this create row put in the newrowpasscount, create another row put in another newrowpasscount..
acing wei 14-Jan-14 4:18am    
I need to save my newrowpasscount when the first loop is done bind in a row of table, then in a second loop save the newrowpasscount again and bine to the following row...
C#
DataTable dtoutput = new DataTable();

// generate the data you want to insert
   DataRow toInsert = ...;

// insert in the desired place
dt.Rows.InsertAt(toInsert, index);


Give it a try.This may help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900