How do a make an <ASP:DataGrid column invisible when its data bound?

J

John Blair

Hi,

Code attached but the line that gives me an error is
MyDataGrid.Columns(2).Visible = False
It actually gives me an error for any value instead of 2 even when 9 bound
columns of data exist. How do i hide a column? Thanks.

MyConnection = New
SqlConnection("server=(local);database=pubs;Trusted_Connection=yes")
MyCommand = New SqlDataAdapter("select * from Authors",
MyConnection)

DS = New DataSet
MyCommand.Fill(DS, "Authors")


MyDataGrid.DataSource = DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
MyDataGrid.BackColor = Color.White
MyDataGrid.BorderColor = Color.Violet
MyDataGrid.BorderStyle = BorderStyle.Dotted
MyDataGrid.ToolTip = "JBs table"

'!!!!!!Error line - out of range althought there are 9 columns.
MyDataGrid.Columns(2).Visible = False


<ASP:DataGrid id="MyDataGrid" runat="server" Width="700"
BackColor="#ccccff" BorderColor="black"
ShowFooter="false" CellPadding="3" CellSpacing="3" Font-Name="Verdana"
Font-Size="8pt" HeaderStyle-BackColor="#aaaadd"
EnableViewState="false" />
 
G

Guest

Since you know that you want to hide Col.2 why dont you set it to
visible=false during the design time?
Hth...
R. Thomas
 
J

John Blair

Thanks for feedback....but as its databound the column does not exist at
design time only run-time! Therefore im looking for a run-time way of hiding
the data (i want to associate data with the row but not show it to the user)
...in case you were going to just say remove it from the SQL statement!
Thanks.
 
G

Guest

Ofcource, No one will ever suggest to remove the col from SQL..
But here is something you can try..
Check if there is actually some data in the datagrid before you try to hide
the col.
Lemme know if there is any data is the datagrid when u are trying to hide
the col.
Hth..
R. Thomas
 
R

Ramesh

This is the first time to give solutions. If any mistakes pls execuse
me.

First, what are the rows u want ot hide,
Using DataTable.Select('condition','order by columns');
Or
Using DataView.Filter() methods you can hide the values.
 
J

John Blair

Thanks for the reply - but i am trying to hide a column not a row.
I want to show ALL rows but only certain columns.
It appears that the nine databount columns dont appear in the
datagrid.columns.count - it shows as 0 instead of 9.

Thanks.
 
J

John Blair

Hi,

There are 23 rows of data in 9 columns but the columns.count is still
showing as 0 - it doesnt seem to synchronsize with the databound data!

thanks.
 
G

Guest

Hi Ramesh, when u user Select or Filter, what you are doin is that you are
seperating the values that you need.
But I believe the purpose of John is to use the hidden col for ID purposes
etc..
 
G

Guest

See, now that is exactly what I was asking, if you have something in the
datagrid to start with.
Now that you know that there is nothin in the datagrid, the error must have
become obvious - there are no cols that you can hide.
I hope now you have got the root cause of ur prob and can rectify it easily
P.S: If this thread was usefull to you, pls click 'Yes' on the top.
Hth..
R. Thomas
 
J

John Blair

Tnanks for the feedback but sorry - i dont believe you understand my
problem.

The grid has no rows or columns initially because it is a databound one.
Once bound via SQL statement there are 23 rows 9 columns.
After data binding i.e. MyDataGrid.DataBind() i want to hide one of those
columns
- i cant do it by making e.g. column(2).visible = false because
..columns.count is still 0.

Hope this is clearer....thanks anyway.
 
E

Eliyahu Goldin

The reason for the error you are getting is that autogenerated columns are
not getting to Columns collection. You have 2 ways of solving the problem.

1. Add the columns in design time. Then you will find them in Columns
collection.

2. Even if the columns are autogenerated, you can handle ItemCreated event
in code-behind. Items corresponds to rows. In the event handler you can
access the cells of the rows you want to hide and make them invisible either
by setting Visible property or by setting a css style rule display:none.

Eliyahu
 
J

John Blair

Very impressed thanks a lot - i chose to use your second method using the
itemcreated event.
Code used to hide the first column for the others who posted to this
subject.

<ASP:DATAGRID id="MyDataGrid" runat="server" OnItemCreated="Item_Created"
....
</ASP:DATAGRID>

Sub Item_Created(sender As Object, e As DataGridItemEventArgs)

e.item.cells(0).visible = false

End Sub 'Item_Created
 
S

Scott Allen

John:

I believe the column count is always 0 if the grid is set to
autogenerate columns...
 
E

Eliyahu Goldin

unless you add some columns manually

Eliyahu

Scott Allen said:
John:

I believe the column count is always 0 if the grid is set to
autogenerate columns...
 
G

Guest

hi,
The solution is simple go to itembound event and put this line
e.item.cells(0).visible = false
It is advisable to make visiblity and stuff in Item databound event

If you r using .net IDE
The second option is to go to the property builder and set the visible check
box of column in columns

happy coding

Regards,
Mehta Rahul
 
R

Ramesh

This is the first time to give solutions. If any mistakes pls execuse
me.

First, what are the rows u want ot hide,
Using DataTable.Select('condition','order by columns');
Or
Using DataView.Filter() methods you can hide the values.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top