我是一名新的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