使用自动生成的列设置网格视图列的位置

本文关键字:视图 位置 网格 设置 自动生成 | 更新日期: 2023-09-27 18:00:36

我有一个自动生成Column = "true"Gridview 现在我想更改网格视图OnRowCreated事件中网格视图列的位置。

我使用此代码

  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 列移动到第一个位置,所以我使用

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

但它不起作用。

使用自动生成的列设置网格视图列的位置

如果要将GridView绑定到DataTable,则可以在将其绑定到GridView之前移动DataTable上的列:

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