C# 在模板字段和绑定字段之间移动gridview中的列

C# 在模板字段和绑定字段之间移动gridview中的列,c#,asp.net,linq-to-sql,gridview,C#,Asp.net,Linq To Sql,Gridview,我有一个gridview(GridviewProduct),其中我有一个模板字段列链接按钮作为“从购物车中删除”,旁边还有一个模板字段列作为“数量”,其他三个字段是boundField,如ProductName、ProductPrice和ProductBrand。如下所示: 我想将quantity列移动到右侧gridview的末尾。当我使用此代码时,它会给我一个错误: 插入索引超出范围 请提供帮助。如果您有5列并删除其中一列,则剩下4列。新的最后一列的索引将大于4(因为它是从零开始的) 我不确

我有一个gridview(GridviewProduct),其中我有一个模板字段列链接按钮作为“从购物车中删除”,旁边还有一个模板字段列作为“数量”,其他三个字段是boundField,如ProductName、ProductPrice和ProductBrand。如下所示:

我想将quantity列移动到右侧gridview的末尾。当我使用此代码时,它会给我一个错误:

插入索引超出范围


请提供帮助。

如果您有5列并删除其中一列,则剩下4列。新的最后一列的索引将大于4(因为它是从零开始的)


我不确定这是否会消除所有问题,但至少这个错误应该会消失…

您的要求可以在Gridview的RowCreated事件中实现

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            List<TableCell> columns = new List<TableCell>();

            //Get the first Cell /Column
            TableCell cell = row.Cells[1];
            // Then Remove it after
            row.Cells.Remove(cell);
            //And Add it to the List Collections
            columns.Add(cell);

            // Add cells
            row.Cells.AddRange(columns.ToArray());
        }
protectedvoid GridView1\u RowCreated(对象发送方,GridViewRowEventArgs e)
{
GridViewRow行=e.行;
列表列=新列表();
//获取第一个单元格/列
TableCell单元格=行单元格[1];
//然后把它拆下来
行。单元格。删除(单元格);
//并将其添加到列表集合中
列。添加(单元格);
//添加单元格
row.Cells.AddRange(columns.ToArray());
}

是否需要第1列和第2列?如果不需要,则首先设置autogeneratecolumn=false@Anuj:当我这样做时,我的边界字段列消失了。。我有一个来自linq2sql的数据源。我会调试代码并检查insert语句中有多少列。。。也许是出了什么问题。谢谢昆丹。。当我在搜索答案时,我遇到了ITemplate界面,它可以从服务器端向gridview添加列。移动列会有帮助吗。。我的意思是这个解决方案很好,但会吗?如果我没有找到你的答案。。我是要去的!
GridViewProduct.Columns.Insert(4, Quantity);
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            List<TableCell> columns = new List<TableCell>();

            //Get the first Cell /Column
            TableCell cell = row.Cells[1];
            // Then Remove it after
            row.Cells.Remove(cell);
            //And Add it to the List Collections
            columns.Add(cell);

            // Add cells
            row.Cells.AddRange(columns.ToArray());
        }