开发者

How to reorder columns in a datagrid Dynamically?

I am using autogeneratecolumns ="false" and using some template fields.

It works when I use:

tempColumnNew = Me.DataGrid.Columns(oldColIndex)
tempColumnOld = Me.DataGrid.Columns(newColIndex)

Me.DataGrid.Columns.RemoveAt(oldColIndex)
Me.DataGrid.Columns.RemoveAt(newColIndex)

Me.DataGrid.Colu开发者_JS百科mns.AddAt(oldColIndex, tempColumnOld)
Me.DataGrid.Columns.AddAt(newColIndex, tempColumnNew)

But it doesnt remember the added columns on postback, so I have to add them for every postback.

Is there a better way to implement it?


Are you using IsPostBack on the Page_Load event?

If Not IsPostBack Then
  tempColumnNew = Me.DataGrid.Columns(oldColIndex)
  tempColumnOld = Me.DataGrid.Columns(newColIndex)

  Me.DataGrid.Columns.RemoveAt(oldColIndex)
  Me.DataGrid.Columns.RemoveAt(newColIndex)

  Me.DataGrid.Columns.AddAt(oldColIndex, tempColumnOld)
  Me.DataGrid.Columns.AddAt(newColIndex, tempColumnNew)
End If


                    This code worked for me:
                    oldColIndex = Me.DataGrid.Columns.IndexOf(col)
                    tempColumnNew = Me.DataGrid.Columns(oldColIndex)
                    tempColumnOld = Me.DataGrid.Columns(newColIndex)

                    Me.DataGrid.Columns.AddAt(oldColIndex, tempColumnOld)
                    Me.DataGrid.Columns.RemoveAt(oldColIndex + 1)
                    Me.DataGrid.Columns.AddAt(newColIndex, tempColumnNew)
                    Me.DataGrid.Columns.RemoveAt(newColIndex + 1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜