DataTable 添加行的两种写法

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Web2.aspx.cs" Inherits="Web2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>测试中……</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
    </div>
    </form>
   
    
</body>
</html>

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Web2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataView dv = new DataView(create());
        dv.RowFilter = "Description in ('c1','c2')";
        DropDownList1.DataSource = dv;
        DropDownList1.DataTextField = "product";
        DropDownList1.DataValueField = "id";
        DropDownList1.DataBind();

        dv = new DataView(create2());
        DropDownList2.DataSource = dv;
        DropDownList2.DataTextField = "Product";
        DropDownList2.DataValueField = "Version";
        DropDownList2.DataBind();
        
    }
    
    protected DataTable create()
    {
        DataTable tblDatas = new DataTable();
        tblDatas.TableName = "tb";
        tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
        tblDatas.Columns[0].AutoIncrement = true;
        tblDatas.Columns[0].AutoIncrementSeed = 1;
        tblDatas.Columns[0].AutoIncrementStep = 1;

        tblDatas.Columns.Add("Product", Type.GetType("System.String"));
        tblDatas.Columns.Add("Version", Type.GetType("System.String"));
        tblDatas.Columns.Add("Description", Type.GetType("System.String"));

        tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
        tblDatas.Rows.Add(new object[] { null, "a1", "b1", "c1" });
        tblDatas.Rows.Add(new object[] { null, "a2", "b2", "c2" });
        tblDatas.Rows.Add(new object[] { null, "a3", "b3", "c3" });
        tblDatas.Rows.Add(new object[] { null, "a4", "b4", "c4" });
        
        return tblDatas;

    }
    protected DataTable create2()
    {
        DataTable tblDatas = new DataTable("Datas");
        DataColumn dc = null;
        dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
        dc.AutoIncrement = true;//自动增加
        dc.AutoIncrementSeed = 1;//起始为1
        dc.AutoIncrementStep = 1;//步长为1
        dc.AllowDBNull = false;//

        //dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
        //dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
        //dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
        tblDatas.Columns.Add("Product", Type.GetType("System.String"));
        tblDatas.Columns.Add("Version", Type.GetType("System.String"));
        tblDatas.Columns.Add("Description", Type.GetType("System.String"));

        DataRow newRow;
        newRow = tblDatas.NewRow();
        newRow["Product"] = "水果刀";
        newRow["Version"] = "2.0";
        newRow["Description"] = "打架专用";
        tblDatas.Rows.Add(newRow);

        newRow = tblDatas.NewRow();
        newRow["Product"] = "折叠凳";
        newRow["Version"] = "3.0";
        newRow["Description"] = "行走江湖七武器之一";
        tblDatas.Rows.Add(newRow);
        
        return tblDatas;

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值