在数据表中添加值 [英] to add values inside the datatable

查看:73
本文介绍了在数据表中添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含四列A,B,C,D的数据表我想在这些数据类型的列中添加整个值int



A B C D

1 0 0 0

0 1 0 0

0 0 0 1





在列中添加这些值,例如:sum = a [i] + b [i] + c [i] + d [i]

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]

推荐答案

访问它们的一种方法是,假设你有一个名为dt的DataTable



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

dt.Rows[0][0].ToString(); // this will give you the first row first column


试试





Try like


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


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


这篇关于在数据表中添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆