zoukankan      html  css  js  c++  java
  • Asp.net DataTable添加列和行的方法

    Asp.net DataTable添加列和行的方法

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

  • 相关阅读:
    Markdown 画 UML 图(六)
    Markdown 高级技巧(五)
    Markdown 链接、图片、表格(四)
    16.3Sum Closet
    15.Three Sum
    11.Container With Most Water
    1.Two Sum
    优化学习笔记5
    优化学习笔记4
    优化学习笔记3
  • 原文地址:https://www.cnblogs.com/liancs/p/3879334.html
Copyright © 2011-2022 走看看