473,382 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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;Truste d_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" />


Nov 19 '05 #1
15 3727
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
"John Blair" wrote:
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;Truste d_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" />


Nov 19 '05 #2
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.

"R. Thomas, aka Xtreme.Net" <NoSpam(Xt**********@hotmail.com.NoSpam> wrote
in message news:EF**********************************@microsof t.com...
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
"John Blair" wrote:
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;Truste d_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" />


Nov 19 '05 #3
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

"John Blair" wrote:
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.

"R. Thomas, aka Xtreme.Net" <NoSpam(Xt**********@hotmail.com.NoSpam> wrote
in message news:EF**********************************@microsof t.com...
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
"John Blair" wrote:
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;Truste d_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" />



Nov 19 '05 #4
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.

Nov 19 '05 #5
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.

"Ramesh" <na**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.

Nov 19 '05 #6
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.

"R. Thomas, aka Xtreme.Net" <NoSpam(Xt**********@hotmail.com.NoSpam> wrote
in message news:24**********************************@microsof t.com...
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

"John Blair" wrote:
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.

"R. Thomas, aka Xtreme.Net" <NoSpam(Xt**********@hotmail.com.NoSpam>
wrote
in message news:EF**********************************@microsof t.com...
> 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
>
>
> "John Blair" wrote:
>
>> 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;Truste d_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" />
>>
>>
>>
>>
>>


Nov 19 '05 #7
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..
"Ramesh" wrote:
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.

Nov 19 '05 #8
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

"John Blair" wrote:
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.

"Ramesh" <na**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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.


Nov 19 '05 #9
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.
"R. Thomas, aka Xtreme.Net" <NoSpam(Xt**********@hotmail.com.NoSpam> wrote
in message news:15**********************************@microsof t.com...
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

"John Blair" wrote:
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.

"Ramesh" <na**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
> 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.
>


Nov 19 '05 #10
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

"John Blair" <jo********@hotmail.com> wrote in message
news:SH*************@newsfe1-gui.ntli.net...
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;Truste d_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" />

Nov 19 '05 #11
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



"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"John Blair" <jo********@hotmail.com> wrote in message
news:SH*************@newsfe1-gui.ntli.net...
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;Truste d_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" />


Nov 19 '05 #12
John:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 27 Dec 2004 11:58:22 GMT, "John Blair"
<jo********@hotmail.com> wrote:
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.


Nov 19 '05 #13
unless you add some columns manually

Eliyahu

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:c3********************************@4ax.com...
John:

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

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Mon, 27 Dec 2004 11:58:22 GMT, "John Blair"
<jo********@hotmail.com> wrote:
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.

Nov 19 '05 #14
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


"John Blair" wrote:
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



"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

"John Blair" <jo********@hotmail.com> wrote in message
news:SH*************@newsfe1-gui.ntli.net...
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;Truste d_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" />



Nov 19 '05 #15
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.

Nov 19 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fresh Air Rider | last post by:
Hi Could anyone please give me the syntax for calling a function with more than one parameter from an <asp:LinkButton> ? Many thanks John
4
by: z. f. | last post by:
Hi, i stated that this is an advanced question because i have a post from few days ago that i received answers to with suggestions that looked good but did not work, so please if you post a...
1
by: roni | last post by:
in datagrid control , how can i change the width of textbox in edit mode (on <asp:BoundColumn> ) ?
4
by: Satya | last post by:
Hi all, The following code is throwing a run time error "The server tag is not well formed. " <ItemTemplate> <asp:HyperLink Runat="server" ID="lnkFile"...
0
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
0
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
2
by: John Smith | last post by:
I have this DataGrid: <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="SM"> <ItemTemplate> <asp:Label id="Label2" runat="server"...
2
by: John Smith | last post by:
I have this DataGrid: <asp:datagrid id="dg1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateColumn HeaderText="SM"> <ItemTemplate> <asp:Label id="Label2" runat="server"...
1
by: khalid sohail | last post by:
hi everybody i want to pass the window size parameter to <asp:hyperlink> attribute Navige URL Property. tell me how to pass the parameter to it.....actually i want to open the new window on the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.