1. 程式人生 > >c#和vb.net建立datatable

c#和vb.net建立datatable

Asp.net DataTable新增列和行的方法
方法一:

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"));

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);

方法二:

DataTable tblDatas = new DataTable("Datas");
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, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });

方法三:
DataTable table = new DataTable ();

//建立table的第一列
DataColumn priceColumn = new DataColumn();
//該列的資料型別
priceColumn.DataType = System.Type.GetType("System.Decimal");
//該列得名稱
priceColumn.ColumnName = "price";
//該列得預設值
priceColumn.DefaultValue = 50;

// 建立table的第二列
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
//列名
taxColumn.ColumnName = "tax";
//設定該列得表示式,用於計算列中的值或建立聚合列
taxColumn.Expression = "price * 0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
//該列的表示式,值是得到的是第一列和第二列值得和
totalColumn.Expression = "price + tax";

// 將所有的列新增到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);

//建立一行
DataRow row = table.NewRow();
//將此行新增到table中
table.Rows.Add(row);

//將table放在試圖中
DataView view = new DataView(table);
dg.DataSource = view;

dg.DataBind();


public static DataTable getDate()
        {
            DataTable td = new DataTable();
            td.Columns.Add("id");
            td.Columns.Add("city");
            string[] str = { "北京市", "天津市", "河北省", "山西省", "內蒙古", "遼寧省", "吉林省", "黑龍江", "上海市", "江蘇省", "安徽省", "福建省", "江西省", "山東省", "河南省", "湖北省", "湖南省", "廣東省", "海南省", "廣西", "甘肅省", "陝西省", "新疆", "青海省", "寧夏", "重慶市", "四川省", "貴州省", "雲南省", "西藏", "臺灣省", "澳門", "香港 " };
            for (int i = 0; i < str.Length; i++)
            {
                DataRow r = td.NewRow();
                r[0] = i;
                r[1] = str[i];
                td.Rows.Add(r);
            }
            return td;
        }

--------------------------

Public Class CreateDataTB
    Public Shared Function NewTB() As DataTable
        Dim tb As New DataTable
        tb.Columns.Add("序號")
        tb.Columns.Add("名稱")
        tb.Columns.Add("型別")
        tb.Columns.Add("日期")
        tb.Columns.Add("備註")
        tb.Rows.Add("1", "aaa", "bigg", "ggggss", "sssss")
        tb.Rows.Add("2", "bbb", "smll", "llllll", "44444")
        tb.Rows.Add("3", "ccc", "dogss", "sssss", "k6666")
        tb.Rows.Add("4", "eee", "yyyr", "343as", "y78ghj")
        Return tb
    End Function
End Class

-------------------------

Dim   ds   As   DataSet
Dim   dt   As   DataTable
Dim   dc   As   DataColumn

Private   Sub   Create_DataTable()
                dt   =   New   DataTable( "PtIn ")

                dc   =   New   DataColumn
                dc.DataType   =   System.Type.GetType( "System.Int32 ")
                dc.ColumnName   =   "ID "
                dc.Unique   =   True
                dt.Columns.Add(dc)

                dc   =   New   DataColumn
                dc.DataType   =   System.Type.GetType( "System.String ")
                dc.ColumnName   =   "全宗號 "
                dt.Columns.Add(dc)

                ......

    ds   =   New   DataSet
                ds.Tables.Add(dt)
                DataGridPtIn.DataSource   =   ds.Tables( "PtIn ")
End   Sub

Private   Sub   FormPtIn_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load
                Create_DataTable()
  End   Sub

Private   Sub   BtnPtIn_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   BtnPtIn.Click
       For   i   =   0   To   4      
                                        Dim   dr   As   DataRow
                                        dr   =   dt.NewRow
                                        dr( "ID ")   =   i
                                        dt.Rows.Add(dr)  
       Next
End   Sub