Hide DataDrig column at run time

R

ruca

Hi,

I want to set visible to false in my datagrid to all fields that are an ID.
How can I do this?
I'm using ItemDataBound event but it gives me an error: "Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index"

I have this code:

---------------------------------------------------------------------------------------------
Sub Item_Bound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Try
Dim i As Integer = 0
Dim lCol As Integer = dg.Columns.Count()
Dim strH As String

For i = 0 To lCol
strH = dg.Columns(i).HeaderText().ToUpper()
If Left(strH, 2) = "Id" Then

dg.Columns(i).Visible = False
End If
Next

Catch ex As Exception
lblMsgErro.Text = ex.Message()
End Try
 
A

Andrea Williams

I believe you need to change you For loop to the following since it is a
zero based array:
For i = 0 To (lCol - 1)

Andrea
 
R

ruca

Right...
This resolve 50% of my problem.

The 50% resolved it was the error disappear.
The 50% non resolved it was the ID columns continue appear and I don't want.
 
E

Eliyahu Goldin

You are using autogenerated columns. They are avilable only in ItemCreated
event. Handle the event and set visibility there. Or don't use autogenerated
columns.

Eliyahu
 
R

ruca

I must have this with autogenerated columns set to true.
With ItemCreated event happens the same thing. I can't get the number of
columns generated automatically
 
E

Eliyahu Goldin

In ItemCreated event don't try to access Columns property. Rather use event
argument that the handler gets as a parameter:
DataGridItemEventArgs e
e.Item gives you a row. It has Cells collection. Set visibility for every
cell.

Eliyahu
 
M

Michael Tkachev

You can't use IntemDataBound for that. You have to use the method that
filling the datagrid.

//here you add you code that makes visible false for columns.

for example.
dgd.Columns[0].Visible = false;

dgd.datasource = your source;
dgd.DataBind();

bye
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top