【问题标题】:Set postion of gridview column with autogenerated column使用自动生成的列设置 gridview 列的位置
【发布时间】:2013-02-23 11:10:06
【问题描述】:

我有一个带有自动生成的Column = "true"Gridview 现在我想更改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 列移动到网格视图的最后位置

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

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

但它不起作用....

【问题讨论】:

  • 你是如何对 gridview 进行数据绑定的?至于什么?
  • 我使用存储过程在 buttonclick 上绑定网格视图
  • 用什么数据结构,datatable?
  • 在将数据表绑定到 gridview 之前,您不能更改列的顺序吗?
  • 你确定.. 我试试... dt.Columns["BookCode"].SetOrdinal(0);它的工作原理。

标签: c# asp.net gridview


【解决方案1】:

如果您将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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多