2017-03-31 124 views
2

我想改变gridview列的顺序(第8位是第一位,第一位是第二位,第二位是第三位......)。我正在使用文本文件中的数据填充我的GridView(其中有9列)。代码是在这里:如何更改gridview中列的顺序?

public DataTable ConvertToDataTable(string filePath, int numberOfColumns) 
    { 
     DataTable tbl = new DataTable(); 
     for (int col = 0; col < numberOfColumns; col++) 
     tbl.Columns.Add(new DataColumn("Column" + (col + 1).ToString())); 

     string[] lines = File.ReadAllLines(filePath); 
     List<string> ekran = new List<string>(); 
     foreach (string line in lines) 
     { 
      var cols = line.Split(','); 
      DataRow dr = tbl.NewRow(); 
      for (int cIndex = 0; cIndex < 9; cIndex++) 
      { 
       if (cols[cIndex].Contains('_')) 
       { 
        cols[cIndex] = cols[cIndex].Substring(0, cols[cIndex].IndexOf('_')); 
        dr[cIndex] = cols[cIndex]; 
       } 
       else 
       { 
        cols[cIndex] += '_'; 
        cols[cIndex] = cols[cIndex].Substring(0, cols[cIndex].IndexOf('_')); 
        dr[cIndex] = cols[cIndex]; 
       } 
      } 
      for (int cIndex = 0; cIndex < 9;cIndex++) 
       if (dr[cIndex].ToString() == promenljiva) 
        tbl.Rows.Add(dr); 
     } 

     this.GridView1.DataSource = tbl; 
     this.GridView1.DataBind(); 

     return tbl; 
    } 

之后,我有这样的:

ConvertToDataTable(filePath, 9); 

这是GridView控件:

<asp:GridView ID="GridView1" runat="server"></asp:GridView> 

我可怎么办呢?

回答

3

之前设置

tbl.Columns[8].SetOrdinal(0); 

网格的DataSource这将改变DataTable.DataColumnCollection内的DataColumn的Ordinal财产的方式,电网认为,作为第一列,您的第9列。当然所有其他列移动到一个新的指数位置