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

public partial class DataTableDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region  添加一个表
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ID", typeof(int)) { DefaultValue = 0 });
        dt.Columns.Add(new DataColumn("Name", typeof(string)) { DefaultValue = "1" }); 
        dt.Columns.Add(new DataColumn("Item", typeof(string)) { DefaultValue = "1" });
        dt.Columns.Add(new DataColumn("ItemAmount", typeof(double)) { DefaultValue = 0 });

        dt.Rows.Add(1, "小李", "基本工资", 60);
        dt.Rows.Add(1, "小李", "业绩提成", 70);

        dt.Rows.Add(2, "小张", "基本工资", 500);
        dt.Rows.Add(2, "小张", "业绩提成", 60);
        dt.Rows.Add(2, "小张", "奖金福利", 800);

        dt.Rows.Add(3, "小王", "基本工资", 30);
        dt.Rows.Add(3, "小王", "奖金福利", 900);
        #endregion

        DataTable dtSource = dt.Copy();

        dt.Columns[3].SetOrdinal(1); //ItemAmount列放置到第二列 索引从0开始
        
        //列移动前
        GridView1.DataSource = dtSource;
        GridView1.DataBind();
        //列移动后
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
}


本文转载:CSDN博客