【问题标题】:asp.net : grid view : arrange columnasp.net:网格视图:排列列
【发布时间】:2011-06-09 11:09:56
【问题描述】:

我正在使用 ASP.NET 网格视图来显示表中的数据。 我想按用户选择的特定顺序排列列。

说明: 我想根据用户的输入交换列(即索引 3 变为 5 等),这可能吗?

我已经尝试过使用这段代码..但它仍然给出了意想不到的结果

        var boundF0 = (BoundField)GVReport.Columns[0];
        var boundF5 = (BoundField)GVReport.Columns[5];

        GVReport.Columns.RemoveAt(0);
        GVReport.Columns.RemoveAt(5);

        GVReport.Columns.Insert(0, boundF5);
        GVReport.Columns.Insert(5, boundF0); 

知道出了什么问题吗?

【问题讨论】:

  • 我认为问题在于您试图操作 Gridview 对象,而不是您绑定到 gridview 对象的数据
  • 您能解释一下意外结果是什么吗?

标签: asp.net gridview


【解决方案1】:

Gridview 可以使用http://www.devexpress.com/ 或以下是传统方法的代码。

DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));

while (dr.Read())
dt.Rows.Add(new object[] { dr[0], dr[1] });

Grid1.DataSource = dt;
Grid2.DataBind();

This would give you a grid with Column1 and Column2. Once you changed
the order you can do following

If (order_changed)
{
dt.Columns.Add("Column2", typeof(string));
dt.Columns.Add("Column1", typeof(string));
while (dr.Read())
dt.Rows.Add(new object[] { dr[1], dr[0] });
}

else
{

dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
while (dr.Read())
dt.Rows.Add(new object[] { dr[0], dr[1] });
}

Now you would get a grid with Column2 and Column1

将grid的autogeneratecolumns属性设置为true,你会得到列

【讨论】:

    猜你喜欢
    • 2010-10-27
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多