In Visual Studio Winforms applications you can set a column to hidden, and still use it to retrieve data from. If you set a column to inviisble in an ASP.NET application, the column is *not* available to get data from. If you set a column to invisible ad design time, the column is never bound to the data, and thus not availavle in your web application. thus, the solution is to set the column to VISIBLE at design time, and hide it during the generation of the actual output. That way – data is bound to the column, and can be used in your application.
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated ' --- Hide column 3 e.Row.Cells(4).Visible = False End Sub
A second step would be to make sure we no longer need to index the cell in the datagrid with the integer number. To be able to reference the cell by name, the following helper function can be used to get the index of the cell by the bound fields name
Public Class GridFieldFinder ''' <summary> ''' Retrieve the integer index of the cell inside the datagrid by supplying the name ''' of the field to locate, and the gridview object ''' </summary> ''' <param name="sColumnname">The name of the column to find</param> ''' <param name="oGrid">The Datagrid object to locate the column in</param> ''' <returns>Integer index of the bound field, or -1 for error</returns> ''' <remarks></remarks> Public Shared Function GetIndexOf(ByVal sColumnname As String, ByVal oGrid As GridView) Dim nReturn As Integer = -1 For i As Integer = 0 To oGrid.Columns.Count - 1 Dim oBoundField As BoundField = oGrid.Columns(i) If oBoundField.DataField = sColumnname Then nReturn = i End If Next Return nReturn End Function End Class