我是一名新的ASP.NET开发人员,我想用HtmlTable
或DataTable
以编程方式创建一个表格,然后使用DataView
填充此表格中来自数据库的数据。如何使用DataView创建DataTable并使用数据库中的数据填充它?
我能创建使用HtmlTable
一个表,但是当我做了关于搜索“如何使用DataView
数据填写HtmlTable
”,我发现,数据视图不会HtmlTable
工作。所以我重复使用DataTable
创建表格,但现在我需要使用DataView
填充数据,那么我该怎么做?
我的代码:
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("Name");
DataColumn col2 = new DataColumn("Username");
col1.DataType = System.Type.GetType("System.String");
col2.DataType = System.Type.GetType("System.String");
table.Columns.Add(col1);
table.Columns.Add(col2);
DataRow row = table.NewRow();
row[col1] = "John";
row[col2] = "John123";
table.Rows.Add(row);
GridView gv = new GridView();
gv.DataSource = table;
gv.DataBind();
关于在数据库中的表,用户表的模式是:
Username, FirstName, LastName, Age, Job