VB.NET from Dummies(3) – On hiding a column in autogenerate gridview

Well, there is some trick added on my sleeves on datagrid manipulation. Well, you know about gridview rite?

The case were, I had an semi-auto generated column, well, that’s because I have one template column I made and the other columns were autogenerated. And, I need to hide certain column because it’s overided with my template column.

Now how can we do that?

Actually…. it’s really simple (well, couple hour googling, looking in forums, looking in msdn, some asking here and there, trying to understand how those event arguments work, and volia).

Exactly how?

It’s simple. There is and event you can catch to to this thing. accessing the gridview itself won’t work because the gridview won’t recognize the autogenerate column. It’s beyond their responsibility.

So… there’s goes the thing called event arguments.

There is an event you can catch, RowDataBound event.

First I tried this:

Private Sub gvEx_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEx.RowDataBound
e.Row.Cells(1).Visible = False
End Sub

Well… In that code I try to make the 1-th cell (the starting index is 0 by the way) when the on the RowDataBound event will be hidden. That code will executed for each row. So… what will happen? Index out of bound! Because the last row the gridview created is an empty column. So… I have to modified that code a bit. I have to make those code executed only when the row that contains item.

So, the final code is:

Private Sub dgPriority_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgPriority.RowDataBound
If e.Row.Cells.Count > 1 Then
e.Row.Cells(1).Visible = False
End If
End Sub

There you have it.

You can change the index with any column you like to hide. You can addition you can manipulate the data, change cells color if their values met certain condition, and other.

Have fun!

About dnial

You don't see anything You don't hear anything You don't know anything Move along and pretend nothing happen

Posted on 23 Juni, 2008, in experience, IT, programming. Bookmark the permalink. 4 Komentar.

  1. We can only see a short distance ahead, but we can see plenty there that needs to be done. ~ Alan Turing

  2. Writing for yourself, dude?😛

  3. di postingannya ada mistypo lho..😀 yg bener bukan volia, tapi voila.. [ga penting :D]

  4. @galih and chiman
    Catatan kecil untuk diri sendiri.😀
    Jadi salah2 gpp …

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: