Hiding columns in the ItemDataBound event without using the column index

Lately I've needed to hide certain columns in DataGrids depending on the specific data source so I create a property at the top of the page like so:

private string DisplayProjDesc

//DisplayProjDesc property - for ItemDataBound event of the DataGrids to determine whether to display project description

{

set

{

this.lblBoolDispProjDesc.Text = value;

}

get

{

return this.lblBoolDispProjDesc.Text.ToUpper();

}

}


Then, in the ItemDataBound event of the DatGrid, I use something like:

if(this.DisplayProjDesc=="TRUE")

{

this.dgProject.Columns[7].Visible=true;

}

else

{

this.dgProject.Columns[7].Visible=false;

}


I realize howver, that if I ever need to add a new column in the middle of the grid, I'd have to go back and change this. Is there an alternative to using the column index?



0
steveba
6/10/2005 8:35:57 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
1111 Views

Similar Articles

[PageSpeed] 9

Hello, the solution flashes in my mind is retrieving the column index by comparing the header text. For instance, if I would like to hide the column with header text "Last Name", then in ItemDataBound event,

   int itemindex = 0;
   
   if(e.Item.ItemType == ListItemType.Header)
   {
         int iTotal = e.Item.Cells.Count;

         for
(int index=0;index<iTotal;index++) 
        {
               if
(e.Item.Cells[index].Text.Equals("Last Name"))
                  itemindex = index; 
         }
   }

    e.Item.Cells[itemindex].Visible = false;



Try this, hope it helps...


Alvin Chooi
Microsoft ASP.NET™ Enthusiast v1.1 / v2.0, Malaysia
Blog : http://alvinzc.blogspot.com
0
alvinz_c
6/11/2005 8:15:24 AM
Thanks for the response Alvin. Yes, I have tried this, but was hoping there was a way to use the actual ID versus the header text, in case I'm ever asked to change column names by users that feel the header is not descriptive, misleading, etc. Is this possible?
0
steveba
6/13/2005 12:30:56 PM
Reply:

Similar Artilces:

Hiding a column in Gridview without using indexes
Is there a better way to hide a column in a gridview than accessing it by index?   gvMatrix.Columns(12).Visible = False This code is just waiting to break the next time someone adds or removes a column.  We've had similar problems with the cells collection.  Every example code snippet I come across uses hard coded indexes - I just think this is terrible style.  Is there a better way - someway to access it by name or string? Hey you can use this below code to hide a column in the  Gridview  <Columns > <asp:BoundField Visib...

Get a Clicked Column Index Integer value for a GridView column using the Sorting Event Handler
I am creating a GridView nested within a UpdatePanel and I got the sorting, paging all down to a sciene using the GridView events handlers. Now, I stumbled to a minor problem that I can't seem to figure out how to go about it. When sorting, I would like the heading color of the clicked sorted column to change (using CSS). I can't seem to dynamically pass the value of the clicked column index to the method. I am using the following reference to change the color of the column but I cannot seem to retreive the value of the clicked index in integer value: GridView1.Columns(trying to...

Searching all columns without using column name
Hi, I am developing a search engine for my application and I need to grab the entire row from the table if I found the search field in any of the columns and the search is not defined to one table. Please let me know how to search all columns in table without using column names indivdually. If the data is in a DataSet, you can access rows and columns using indexes: myDS.Tables[0].Rows[0][0] will get the first row, first column. .Rows and .Columns have a .Count property that can allow you to get all columns and rows: if ( myDS.Tables.Count>0 ) { for ( int loop...

Reference gridview column index using column headers
 Greetings,I have several GridViews all showing different collections of similar product data.  Due to changes in codes used to distinguish properties such as color and size, I'm having to write several rules into the row databound events.  The issue I'm having is that the group who will be using the application haven't finalized the order of the columns, so everytime I reorder the columns, I have to changes all the column indexes in my code behind so that the rules are being applied to the right columns. I'd like to know if there is a way to reference the colu...

how to fetch column using column index in sql server.
Hi, I have several columns in my table, but I want to fetch data using column index instead of column name in select  command. is there any way to do this.Please remember to click “Mark as Answer” on the post that helps youJasim AkhtarNew Delhi ( INDIA ) Yes there is.It's called dynamic SQL and it's a complicated process.See http://www.sommarskog.se/dynamic_sql.html Please click "Mark as Answer" if my reply solved your problem.CheersRammohan...

Hide a Gridview column but use the value in that column when the row is selected.
I need to retrieve a column from my database but do not want to display it in the gridview.  However, I need to be able to show that information later in the application.  I am using the wizard control.  I believe I can use the DataKeyValues property of the gridview but can't figure out how to write the code to get the value of the cell in that column when the row is selected so it can be referenced later.  I am using VB.  Thanks for your help!! Found the answer.  Here it is in case anyone else needs it:       &nbs...

When is a column not a column?
I have several extremely similar datawindows that I have to maintain that go against the same table. Recently, I had to add a column to that table, and also add the column to ALL of these datawindows that go against this table. I added the column to the first datawindow, set up the edit control style, etc. I then did a copy/paste of the columns edit control to the remaining dw's (of course remembering to first add the column in SQL). Everything (update props, column specs, column props) seems to indicate that the pasted column is linked to the column I selected, HOWEVER, when I...

How to set visible property of Gridview columns using column name instead of index
How to  set visible property of Gridview  columns using column name instead of index Dear janacpp, the columns collection of gridview has no indexer for column name. So you can do something like that:  in your code section you want to set the visible property add this code : GridView1.Columns[GridView1.Columns.IndexOf(GetField("FieldName"))].Visible=false;  where FieldName is the name of your field. and add this function:DataControlField GetField(string name) {DataControlFieldCollection fields = GridView1.Columns.CloneFields();for (int i = 0; i < f...

grid view-hide column after add new hyperlink column
I define a grid and bound a data with one column - "productName" after that I add hyperlinkfiled to the grid - now I can see 2 columns - the new colums dispaly first now I try to hide the original column "productname" but i have an error : out of range here is my code:GridView gv = new GridView(); HyperLinkField Hyperlink1 = new HyperLinkField(); gv.AutoGenerateColumns = false;  ;;<--  I enter this line - I hope it help - but is not help.  gv.DataSource = category.Products;   //add hyper link field to gridgv.DataKeyNames = new string[] { &q...

Datagrid:--> Adjusting column width in edit mode without using template column?
Hello All, I have a datagrid that is populated with data from a database programmatically. It is currently equipped with an Edit command column. When the edit button is clicked the datagrid expands as the update, cancel, and edit-textboxes appear in the grid. I have currently been binding my datasource to the grid without using template or bound columns. Is there a way to maintain my initial datagrid size even when the edit command button is clicked or do I have to reprogram my datagrid with template columns? If I could get some assistance in this area it would be great!! Can you paste...

Use DataBinder.Eval(Object, String) Method with the column index, not the column's name
I can save a lot of processing if I can use the Column Index instead of the Column Name: <%#DataBinder.Eval(CType(Container, DataGridItem).DataItem, <Column Index number here instead>)%>  Is it possible? then you can directly use <%# Container.DataItem(0)%>, <%# Container.DataItem(1)%> Raghu(MCSD.NET, MCAD.NET, MCDBA)[Don't forget to click on Mark as answer on the post that helped you ] Thanks Raghu ! I did not realise that "Container" refered to the DataSource like that. Raghu, do you know how to get the name of the column from <%# Cont...

Write binary date into Blob column without using ODP.NET
 Currently I use Enterprise Library 2.0 to access the Oracle 9i. Everything works fine except when I need to insert image into Blob column when OracleDbType is not a option to be used. I noticed that when I pass in the image as binary to proc as RAW, only those upto 32k in size can be inserted.  Is that possible that I can I chunk the binary data (byte[])  and pass to the proc in a loop, then use dbms_lob.writeappend(. . .)?  I know this is possible with CLOB columns though Please advise, Thanks, MJMJ Hey MJ, Yeah, the limit on parameters is 32k.....

Hiding a column in a GridView
Hi I am trying to hide a column of the gridview. I came across these 2 solutions RowCreated event - GridView Hidden Column Problem (And Two Common Solutions)RowDataBound event -  Hide a GridView column at runtime Both mentioned the same way of hiding a column but in different event handlers.I did the same in my case, but hiding the column gives me a blank header andaffects the other columns by displaying (dragging) the data in wrong columns. The gridview ( before hinding the User Id column ) CheckBox    UserId      Name  ...

multi-column HG index vs. multiple single-column HG index
In IQ 12.4.3, HG index could be created upon multiple columns. My questions are 1) in terms of query performance, which one is better -- one multiple-column HG index or multiple single-column HG indexes? 2) And what about the load performance? 3) If one column is part of a multi-column HG index, do it make sense to create a seperate HG/LF index for it? Thanks in advance, Richard Mei Number one, I don't know (great start, eh?). Speaking from ignorance, though, I would bet the following are all true: (1) If your HG index columns correspond exactly to your group ...

Web resources about - Hiding columns in the ItemDataBound event without using the column index - asp.net.presentation-controls

Resources last updated: 1/18/2016 7:48:41 AM