当前位置:编程学习 > C#/ASP.NET >>

C# 编程 dt.rows[i] 如何反射出DataTable中行的属性和属性对应的值

补充:如何反射出dt.rows[i]中的属性和属性分别对应的值,貌似用GetType().GetProperties() 可是没用弄出来,求高手帮忙解决一下!
追问:我的意思是在我不知道字段名的情况下,如何反射出那个字段名和对应的值!
答案:Create one project,and try this code ,good luck

 

static void Main(string[] args)
        {
            //Assembly aa = Assembly.Load(
            DataTable table = new DataTable();
            DataColumn col = new DataColumn("userNo", typeof(Int32));
            DataColumn col1 = new DataColumn("userName", typeof(String));
            table.Columns.Add(col);
            table.Columns.Add(col1);
            DataRow row = table.NewRow();
            row[0] = 82844219;
row[1] = "www.30c.org";
            table.Rows.Add(row);
            foreach (DataRow trow in table.Rows)
            {
                foreach (DataColumn tcol in table.Columns)
                {
                    Console.WriteLine("get your columnname:{0}", tcol.ColumnName);
                    Console.WriteLine("get your columvalue:{0}", trow[tcol.ColumnName]);
                }
            }
        }

如果你想取某一列的值,可以用下面的这个方法

dt.rows[i]["字段名"].ToString();

dt.rows[i][dt.Columns[j].ColumnName]

j 为列序号

上一个:一道关于C#的编程题目
下一个:C#初级编程·解决下

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,