asp.net(c#版)添加DataRow数据列到DataTable控件

发布时间:2020-10-16编辑:脚本学堂
本文介绍下,在asp.net中添加DataRow数据列到DataTable控件的方法,有需要的朋友参考下。

asp.net实现添加DataRow数据列到DataTable控件。
代码:
 

复制代码 代码示例:

<%@Import namespace="System.Data" %>
<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
    datagrid.DataSource = GetData();
    DataBind();
}

DataTable GetData() {
    DataTable data = new DataTable();
    data.Columns.Add(new DataColumn("TheID", typeof(Int32)));
    data.Columns.Add(new DataColumn("Name", typeof(string)));
    data.Columns.Add(new DataColumn("TimeZone", typeof(string)));

    DataRow dr;
    dr = data.NewRow();
    dr[0] = 1; dr[1] = "Washington"; dr[2] = "Pacific";
    data.Rows.Add(dr);
    dr = data.NewRow();
    dr[0] = 2; dr[1] = "Utah"; dr[2] = "linuxjishu/9952.html target=_blank class=infotextkey>mountain";
    data.Rows.Add(dr);
    dr = data.NewRow();
    dr[0] = 3; dr[1] = "Wisconsin"; dr[2] = "Central";
    data.Rows.Add(dr);

    return data;
}
</script>
<form runat="server">
<asp:DataGrid runat="server" id="datagrid" />
</form>