我有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);
但它没有用......
答案 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}}