看MSDN例子是:
DataTable workTable = new DataTable("Customers");DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));这个是没有数据之前添加,要是有了数据,而且不知道具体是哪一列。怎么添加。其实就是想在DataTable前加一个序列的列,好像行号之类的东西。如何做。

解决方案 »

  1.   

    我也不是很明白,那你就在最前面再加一列,用来记录行号不就行了吗? 不太懂...————————————————————————————————————————
    昏,我就是不知道怎么加,难道for循环一行行的加?
      

  2.   

    没必要在DataTable中指定列,你在读的时候按你要求的就行!
    DataGrid中到可以指定
    DataGrid1.Columns.AddAt("添加的索引","你的列");
      

  3.   

    谢谢 sjc0(流浪者) 但我这边是模块做的。。经理要我在DataTable里面加DataGrid不关我的事
    怎么办?
      

  4.   

    DataTable customerTable = ds.Tables[0];
    DataColumn myDataColumn= customerTable.Columns.Add("ID", System.Type.GetType("System.Decimal"));
       int i=0;
       foreach( DataRow row in customerTable.Rows )
       {
        row["id"]= i++;
       }    
      

  5.   

    谢谢楼上的,这个可以。
    但是我想把它插到第一列,非要用for做吗?有没有其他办法?
      

  6.   

    DataRow dr;
                dr = customerTable.NewRow();
                dr[0] = 10;
                dr[1] = "Item 10" ;
                dr[2] = DateTime.Now;
                dr[3] = true;
                dr[4] = 1.23 * 11;
    customerTable.Rows.InsertAt(dr,0);
    customerTable.AcceptChanges();
    然后再增加ID列
      

  7.   

    xrll() ....看不懂你写的....
      

  8.   

    hehe,
    Of course, Add Column first, since Columns will determine the structure of the DataTable
    And then, add new row into the DataTable in terms of its column structure.
    However, I don't think the order of the columns in DataTable is very important, it is just a collection.You can adjust the display order when you bind DataTable to DataGrid or other DataBind controls
      

  9.   

    应该先增加列,后增加行!插到指定位置我还没找到一种很好的办法(for循环不是很好)!