构建DataTable
Posted whatarey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构建DataTable相关的知识,希望对你有一定的参考价值。
【博客原因】数据一直来自于数据库,造成本地构建Datatable 当做数据源不会了,特此记录一下
DataTable dt = new DataTable(); //构建列 dt.Columns.Add(new DataColumn("id")); dt.Columns.Add(new DataColumn("ServerID")); dt.Columns.Add(new DataColumn("SN")); dt.Columns.Add(new DataColumn("city")); dt.Columns.Add(new DataColumn("cur_name")); dt.Columns.Add(new DataColumn("RoadType")); dt.Columns.Add(new DataColumn("visitRange")); dt.Columns.Add(new DataColumn("lastDate")); dt.Columns.Add(new DataColumn("bstationName")); dt.Columns.Add(new DataColumn("setupnum")); dt.Columns.Add(new DataColumn("setupattach")); dt.Columns.Add(new DataColumn("purchasefrom")); dt.Columns.Add(new DataColumn("setupNo")); dt.Columns.Add(new DataColumn("purchasewarrant")); dt.Columns.Add(new DataColumn("statename")); dt.Columns.Add(new DataColumn("stationID")); dt.Columns.Add(new DataColumn("outModel")); dt.Columns.Add(new DataColumn("inmodel"));
循环遍历,添加数据 for (int i = 0; i < 100; i++) DataRow dr1 = dt.NewRow(); dr1["id"] = "1708"; dr1["ServerID"] = "11111111111111111"; dr1["SN"] = "BVPABYA003913-1"; dr1["city"] = "福州市"; dr1["cur_name"] = "测试"; dr1["Roade"] = "邮件"; dr1["visit"] = "正常上门"; dr1["lastDate"] = "2011-12-06"; dr1["bstationName"] = "总部"; dr1["setu"] = ""; dr1["serattach"] = ""; dr1["chase"] = "其他"; dr1["setNo"] = ""; dr1["purcha"] = "其他"; dr1["StateName"] = "已分派"; dr1["StationID"] = "000000"; dr1["outModel"] = "LC32H03K"; dr1["model"] = "E312AGKNDW3A6NNX"; //将构建好的行加进DataTable中 dt.Rows.Add(dr1); GridView1.DataSource = DT; GridView1.DataBind();
以上是关于构建DataTable的主要内容,如果未能解决你的问题,请参考以下文章