I have several sqldatasources for my gridview. All of the columns are 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!
+1
A:
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.
Carter
2010-08-19 17:04:48
Is there a way to set the visibility by their name? Unfortunately while the columns are consistent, they're not always in the same position.
Shawn
2010-08-20 17:11:32
Get Index by name: Dim x As IntegerDim y As Data.DataRowViewy = e.Row.DataItemx = y.Row.Table.Columns.IndexOf("Status")Unfortunately setting the visibility to false only does it for that row, so all the columns become mismatched. I'll have to figure out a way to hide the entire column instead of the cell.
Shawn
2010-08-23 17:23:09