How to hide a column in datagrid with AutoGenerateCol=true

T

Thanh Nu

Hi,

I would like to hide a column in a web datagrid (with
create columns automatically at runtime checked), and I
cannot refer to the columns collection like this:
DataGrid1.Columns(0).Visible = False
(the message at runtime is something like index out of
range, and under the debuger, I discover that the
attribute count of the columns collection is 0!)
Below is my piece of code.
Could someone help me to solve this problem without
having to bind the datagrid manually, because I have a
sort of generic application, in which I cannot know in
advance the column name.
(Remark : in the catch part, I can manage to hind the
data of the column, but I cannot hide the header, and all
my data columns are shifted left)
Thanks in advance,

------------- Code ------------------
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim msg As String
'Put user code to initialize the page here
SqlConnection1.Open()
Dim rq As String = "select * from Categories"
Dim cmd As New SqlCommand(rq, SqlConnection1)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim objItem As DataGridItem
Dim ThisBackColor As New System.Drawing.Color
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Try
DataGrid1.Columns(0).Visible = False
Catch ex As Exception
For Each objItem In DataGrid1.Items
objItem.Cells(0).Visible = False
Next
End Try
End Sub
 
G

GSK

declare a variable in your page.
int HideColumn;

Turn the visibility of required column to Off on
ItemBound

public void OnItemBound(Object Sender,
DataGridItemEventArgs e)
{
// Get the newly created item
int i;
ListItemType itemType = e.Item.ItemType;

if(HideColumn >=0 && HideColumn<e.Item.Controls.Count)
e.Item.Cells[HideColumn].Visible = false;
}
 
T

Thanh-Nu

Thank you very much!
It works !!!
-----Original Message-----
declare a variable in your page.
int HideColumn;

Turn the visibility of required column to Off on
ItemBound

public void OnItemBound(Object Sender,
DataGridItemEventArgs e)
{
// Get the newly created item
int i;
ListItemType itemType = e.Item.ItemType;

if(HideColumn >=0 && HideColumn<e.Item.Controls.Count)
e.Item.Cells[HideColumn].Visible = false;
}
-----Original Message-----
Hi,

I would like to hide a column in a web datagrid (with
create columns automatically at runtime checked), and I
cannot refer to the columns collection like this:
DataGrid1.Columns(0).Visible = False
(the message at runtime is something like index out of
range, and under the debuger, I discover that the
attribute count of the columns collection is 0!)
Below is my piece of code.
Could someone help me to solve this problem without
having to bind the datagrid manually, because I have a
sort of generic application, in which I cannot know in
advance the column name.
(Remark : in the catch part, I can manage to hind the
data of the column, but I cannot hide the header, and all
my data columns are shifted left)
Thanks in advance,

------------- Code ------------------
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim msg As String
'Put user code to initialize the page here
SqlConnection1.Open()
Dim rq As String = "select * from Categories"
Dim cmd As New SqlCommand(rq, SqlConnection1)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim objItem As DataGridItem
Dim ThisBackColor As New System.Drawing.Color
DataGrid1.DataSource = dr
DataGrid1.DataBind()
Try
DataGrid1.Columns(0).Visible = False
Catch ex As Exception
For Each objItem In DataGrid1.Items
objItem.Cells(0).Visible = False
Next
End Try
End Sub
.
.
 

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,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top