How to hide a column in datagrid with AutoGenerateCol=true

  • Thread starter Thread starter Thanh Nu
  • Start date Start date
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
 
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;
}
 
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
.
.
 
Back
Top