Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created an data table with four columns A,B,C,D i want to add the entire values inside those columns which are of datatype int

A B C D
1 0 0 0
0 1 0 0
0 0 0 1


to add those values inside the column for eg:sum=a[i]+b[i]+c[i]+d[i]
Posted
Updated 7-Aug-13 1:51am
v2

One way to access them would be the following, assuming you have a DataTable named dt

C#
dt.Rows[0][0].ToString(); // this will give you the first row first column
 
Share this answer
 
Try like


C#
DataTable table = new DataTable();
       table.Columns.Add("A", typeof(string));
       table.Columns.Add("B", typeof(string));
       table.Columns.Add("C", typeof(string));
       table.Columns.Add("D", typeof(string));
        table.Rows.Add("0","0", "0","0");
       table.Rows.Add("0","1", "0","0");
        table.Rows.Add("0","0", "0","1");
        int sum = 0;
        foreach (DataRow dr in table.Rows)
        {

            foreach (DataColumn dk in table.Columns)
            {

                sum = +Convert.ToInt32(dr[dk]);
            }
        }
 
Share this answer
 
DataTable dt = new DataTable("Data") ;
            DataColumn dc;
            
            dc = new DataColumn("A");
            dt.Columns.Add(dc);
            dc = new DataColumn("B");
            dt.Columns.Add(dc);
            dc = new DataColumn("C");
            dt.Columns.Add(dc);
            dc = new DataColumn("D");
            dt.Columns.Add(dc);

            DataRow dr;
            dr = dt.NewRow();
            dr["A"] = 1;
            dr["B"] = 0;
            dr["C"] = 0;
            dr["D"] = 0;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["A"] = 0;
            dr["B"] = 1;
            dr["C"] = 0;
            dr["D"] = 0;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["A"] = 0;
            dr["B"] = 0;
            dr["C"] = 0;
            dr["D"] = 1;
            dt.Rows.Add(dr);
 
Share this answer
 
you have to iterate for all rows in your column. try this:-

DataRow row;

for (int i = 0; i <= length; i++) 
{
  row = workTable.NewRow();
  row[0] = "CustID" + i.ToString();
  Table_Name.Rows.Add(row);
}
 
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