Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dtGetList = new DataTable();
            dtGetList.Columns.Add("Sl No").AutoIncrement = true; 
            dtGetList.Columns.Add("VNo");
            dtGetList.Columns.Add("VDate");
            dtGetList.Columns.Add("VName");
            dtGetList.Columns.Add("Amount");
            int SlCount = 1;

            DataRow drGetList = dtGetList.NewRow();

            foreach (DataRow dr in dsGetlist.Tables["GetList"].Rows)
            {
                drGetList["Sl No"] = SlCount;
                drGetList["V1"] = dr["VNo"];
                drGetList["V2"] = dr["VDate"];
                drGetList["V3"] = dr["VName"];
                drGetList["CA"] = Convert.ToDecimal(dr["amt"]);

                dtGetList.Rows.Add(drGetList);//Here i'm Getting error When 2nd time loop through
                SlCount++;
               
            }
            datagrid.DataSource = dsGetlist.Tables["GetList"].Columns[0].AutoIncrement = true;
Posted
Updated 14-Aug-15 0:11am
v2

1 solution

You can't add the same row twice. You need to create a new row for each row to be added. In other words place the NewRow call inside the loop:
C#
DataRow drGetList;
 
foreach (DataRow dr in dsGetlist.Tables["GetList"].Rows)
{
    drGetList = dtGetList.NewRow();

    drGetList["Sl No"] = SlCount;
    drGetList["V1"] = dr["VNo"];
    drGetList["V2"] = dr["VDate"];
    drGetList["V3"] = dr["VName"];
    drGetList["CA"] = Convert.ToDecimal(dr["amt"]);
  
    dtGetList.Rows.Add(drGetList);//Here i'm Getting error When 2nd time loop through
    SlCount++;
              
}
 
Share this answer
 
v3

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