Here we going to discuss about the gridview columns reorder process when the Auto Generated Columns is true. By the time we need to specify a particular gridview cell and have to remove it and then have to add it in the same gridview. This all have to be done with the Gridview rowdatabound event like the following code snippets.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
List<TableCell> cells = new List<TableCell>();
foreach (DataControlField column in GridView1.Columns)
{
//Getting first Column of the Gridview
TableCell cell = row.Cells[0];
//Remove that cell from the gridview
row.Cells.Remove(cell);
//Adding that cell as the last cell in the gridview
cells.Add(cell);
}
// Add cells
row.Cells.AddRange(cells.ToArray());
}
VB.NET Code:
Imports System.Collections
Imports System.Collections.Generic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim row As GridViewRow = e.Row
Dim cells As New List(Of TableCell)()
For Each column As DataControlField In GridView1.Columns
'Getting first Column of the Gridview
Dim cell As TableCell = row.Cells(0)
'Remove that cell from the gridview
row.Cells.Remove(cell)
'Adding that cell as the last cell in the gridview
cells.Add(cell)
Next
' Add cells
row.Cells.AddRange(cells.ToArray())
End Sub
End Class
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.