Friday, April 24, 2009

GridView Hide Column



GridView is used in showing information in a tabular form, GridView replaced the Datagrid which was used in asp.net 1.1, if you are working in asp.net 2.0
then you are facing a problem which is that you can not hide a particular column if you want but in asp.net 1.1 you can do it by simply making the visible
property false,hat it so simple but in asp.net 2.0 its not so easy, but if problem is there, chances of solution is also there

I will tell you 2 method to deal with this type of problem

1). Create the Row Created event handler and do the following:

Public Sub myGrid_OnRowCreated(ByVal sender As Object, ByVal e As Web.UI.WebControls.GridViewRowEventArgs) Handles myGrid.RowCreated

'Those columns you don't want to display you config here,

'you could use a for statement if you have many :)

e.Row.Cells(1).Visible = False

End Sub


2) Public Sub myTestFunction()

'To hide a column, set its width to zero or use MappingType

Dim strCON As String = "<Connection String>"

Dim strQuery As String = "<QueryString>"

Dim da As Data.SqlClient.SqlDataAdapter

Dim ds As Data.DataSet

Try

Dim conn As New Data.SqlClient.SqlConnection(strCON)

da = New Data.SqlClient.SqlDataAdapter(strQuery, CON)

ds = New Data.DataSet

da.Fill(ds, "tblData")

conn.Close()

Catch ex As Exception

'Do error handling here...

End Try

'Here you can HIDE the Column

ds.Tables("tblData").Columns(colIndex).ColumnMapping = Data.MappingType.Hidden

myGrid.DataSource = ds.Tables("tblData")

End Sub

by use these methods we can hide the columns of GridView as we wish.

Happy coding