使用自动生成的列设置gridview列的位置

时间:2013-02-23 11:10:07

标签: c# asp.net gridview

我有Gridview自动生成Column = "true"现在我想在gridview的OnRowCreated事件中更改gridview列的位置。

我使用此代码

  TableCell cell = e.Row.Cells[1];
  TableCell cell1 = e.Row.Cells[0];
  e.Row.Cells.RemoveAt(1);
  e.Row.Cells.RemoveAt(0);
  e.Row.Cells.Add(cell1);
  e.Row.Cells.Add(cell);

它工作正常,它将第0列和第1列移动到网格视图的最后位置

现在我想将第3列gridview移动到第一个位置,所以我使用

TableCell cell2 = e.Row.Cells[3];
e.Row.Cells.RemoveAt(3);
e.Row.Cells.AddAt(0, cell2);

但它没有用......

1 个答案:

答案 0 :(得分:0)

如果您将GridView绑定到DataTable,则可以在将DataTable绑定到GridView之前移动DataTable table = new DataTable(); table.Columns.Add("x"); table.Columns.Add("y"); table.Columns.Add("z"); table.Rows.Add("1", "2", "3"); table.Rows.Add("1", "2", "3"); table.Rows.Add("1", "2", "3"); //Move the column table.Columns["z"].SetOrdinal(0); string value = table.Rows[0][0].ToString(); //Outputs 3 gridView.DataSource = table; gridView.DataBind(); 上的列:

{{1}}