DataGrid Template column question

hi

this looks like simple..but i cant figure out a way to do it...propably some one can here can help me with that.

here is what i want to be able to do

basically i need to hide a column in datagrid..but i dont know which column but i know the control that column has...

//the below code is not right but i need to do something like that
 DataGrid1.Columns[e.Item.FindControl("btnEdit")].Visible=false;

and i dont want to enter column index bcz...i have columns generated automatically and i have teh template columns to the right (i dont want to move to left)....so the column # chages depending on the # of columns dataset has ...

so let me knwo...
thanks

LVL 15
praneethaAsked:
Who is Participating?
 
RamuncikasConnect With a Mentor Commented:
Hey, praneetha

Try using this logic:
If your datagrid autogenerates colums and your template column to be hidden is to the right oh them, then you have to hide the last (column to the most right) of your datagrid.

Here is a solution how to hide the column most to the right even if it is a template column:

'this will hide first column from the right
CType(dg.Columns(dgColumns.Count - 1), TemplateColumn).Visible=False

'this will hide second column from the right
CType(dg.Columns(dgColumns.Count - 2), TemplateColumn).Visible=False

'and so on

This code will prevent datagrid from rendering the most right(second form the right) column of the datagrid. All you need to do is just put this statement in your code, sorround it with IF contition determining waether you need to hide the column or not.

Hope this will help
Ramuncikas
0
 
TorrwinCommented:
I know your index is changing, but does your column have a unique name?  Like: datagrid1.columns("CustomerID").visible = false
0
 
praneethaAuthor Commented:
well that line i wrote is in itemdatabound..but it does have to be in datagrid i guess...like i can hide them dependng on the proveleges of user...like right after binding the data to the datagrid.

Thanks
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
praneethaAuthor Commented:
that is what my column look slike..where would i give it name

<asp:TemplateColumn >
                  <ItemTemplate>
                        <asp:LinkButton Runat="server" CommandName="Edit" ID="btnedit" Runat="server">Edit</asp:LinkButton>
                  </ItemTemplate>
0
 
praneethaAuthor Commented:
hmm looks like a good idea...thanks..

so there is no way to find out which column the control is in..like i know there is "btnedit" and want to know which column it is in...

hey but thats the good idea...any more ideas...

thank you
0
 
praneethaAuthor Commented:
hey torrwin..how do i find the column name or give it a name...
0
 
RamuncikasCommented:
Praneetha,

I don't know if your code is very special, but practically template columns appear ONLY to the right of autogenerated columns.

If you'd explain why it is so important to know WHAT colunm contains the button, I'd try to help.

Ramuncikas
0
 
TorrwinCommented:
Well, I have been working mainly with VB.NET, and not ASP.NET lately, so I assumed it was part of the column.  

However, I have been looking at one of my old projects, and the only thing I could see is trying to set the header text and then try that.

<asp:TemplateColumn HeaderText="myColumn">
               <ItemTemplate>
                    <asp:LinkButton Runat="server" CommandName="Edit" ID="btnedit" Runat="server">Edit</asp:LinkButton>
               </ItemTemplate>


datagrid1.columns("myColumn").visible = false
0
 
praneethaAuthor Commented:
but

DataGrid1.Columns[int index].Visible=false or true...

loks like columns will only take index...not the name..it throws a compile time error
0
 
praneethaAuthor Commented:
but

DataGrid1.Columns[int index].Visible=false or true...

loks like columns will only take index...not the name..it throws a compile time error
0
 
praneethaAuthor Commented:
and for template columns being right.....

:) yep u r right..they always on right...

i actually do not have all autogenerae columns..i have all of them as template columns an di just wanted to explain why i dont want to use column index..i mean i just thought it would ne nicer way to refer it by edit column or something..instead of changing the code when i add more columns later..and i wa sjust not thinking when i said i dont want it on left..:) andmy code is not speical though..well prpably we can do it it that way too..but nope not my code right now...
0
 
RamuncikasCommented:
Referencing a column by a control found in it just complicates the code and longens the run-time.

On the other hand you could just have some constants or an enum for columns you are going to reference

Ramuncikas
0
 
praneethaAuthor Commented:
yep i guess i am convinced with the soln u have given...will leave it open for few more hours before i accept your soln.

Thank You
0
 
RamuncikasCommented:
So, Praneetha,

How is it going with template columns? :)

Ramuncikas
0
 
praneethaAuthor Commented:
sorry totally forgot about it.

Thanks...and thanks torrwin too
0
 
RamuncikasCommented:
Pleased to hear that I could help :)

Good luck
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.