I have several sqldatasources for my gridview. All of the columns ar开发者_运维技巧e autogenerated. However they all have some consistent fields and I'd like to make those fields template fields so I can modify the edit template for them such as adding a drop down menu. Is this possible? If so, how? :-D Thanks!
To replace specific autogenerated columns with template columns, simply define the template column and hide the autogenerated column in code. The autogenerated columns will by default appear after your template columns, so if you want them to be appropriately placed, you can swap the output in code as well.
In this example I am altering the output for a gridview that has two template columns and two autogenerated columns for a total of 4. I want to replace the last of my autogenerated colums (index 3) with a template column (index 1), but I want one of my autogenerated columns (index 2) to be further to the left, so I switch it with a template column (index 1).
Private Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
'hide Column 3'
e.Row.Cells(3).Visible = False
'Swap the results for Column 1 and Column 2'
Dim swap = e.Row.Cells(1).Text
e.Row.Cells(1).Text = e.Row.Cells(2).Text
e.Row.Cells(2).Text = swap
End Sub
This is fairly simple and doesn't require any extra classes.
精彩评论