How to put some hidden column in a DataGrid?

G

Guest

hi, all
I got another quesiton, in my DataGrid control, I want to have a hidden
column which will contian an ID value, I don't want to display this ID value
to user, but I need it when user click it. The problem I got is when I
uncheck the "visible" checkbox, it seems DataGrid does not render it at all.
So how to do this hidden column?
Thanks
 
S

Shawn

Hi.
I always put my ID's in a label:
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label id="lblID" runat="server" Visible="False" Text='<%#
DataBinder.Eval(Container.DataItem, "id") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>

You can get your ID's from codebehind, like this:
For Each dgi In arrayListDataGridItems
..
myID = CType(dgi.FindControl("lblID"), Label).Text
..
Next

Hope this helps,
Shawn
 
G

Guest

Hi Nicky,

The autogenerated coulmns of DataGrid are not part og the columns
collection...so you can't hide those columns just as follows

MyDataGrid.Columns(1).Visible = False

So the place to handle this is in the ItemDataBound event of the Datagrid:

<asp:DataGrid id="MyDatagrid" runat="server" AutoGenerateColumns="True"
OnItemDataBound="MyDatagrid_OnItemDataBound"/>

Private Sub MyDataGrid_ItemDataBound(s As Object, e As DatagridItemEventArgs)
e.Item.Cells(1).Visible = False
End Sub


Cheers,

Jerome. M
 
G

Guest

Hey Nicky,

If you are defining your columns in the ASPX page then try this out :-

<Columns>
<asp:BoundColumn Visible="False" DataField="CandidateId"
HeaderText="Candidate Id">
<ItemStyle Height="10px" Width="10px"></ItemStyle>
</asp:BoundColumn>
<asp:ButtonColumn Text="Select" DataTextField="FullName"
SortExpression="FullName" HeaderText="Full Name">
<ItemStyle Width="300px"></ItemStyle>


and you can access the first column by

candidateNo = e.Item.Cells[0].Text ;

*****************************
Hope This Helps,
Shaun (M.C.P)

http://blogs.wwwcoder.com/shaunakp
*****************************
 
E

Eliyahu Goldin

Thta's right, if Visible=false the column doesn't get rendered. Hide it with
css style display:none.

Eliyahu
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top