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 column index by using the header text, so that regardless of column order changes, my code will point to the right column.

Example of my code:

 ' Corrects color code "O" change from "Coral" to "Moss" in 2005
            If e.Row.Cells(4).Text = "Moss" Then   'If cell in "color" column reads "Moss"
                If e.Row.Cells(5).Text < "2005" Then    ' and cell in "year" column reads < "2005"
                    e.Row.Cells(4).Text = "Coral"     ' then change color to "Coral"
                End If
            End If

 

Now if I add a new column to the beginning of the gridview, I have to change all of my index values:

 ' Corrects color code "O" change from "Coral" to "Moss" in 2005
            If e.Row.Cells(5).Text = "Moss" Then   'If cell in "color" column reads "Moss"
                If e.Row.Cells(6).Text < "2005" Then    ' and cell in "year" column reads < "2005"
                    e.Row.Cells(5).Text = "Coral"     ' then change color to "Coral"
                End If
            End If 

If anybody knows how to use the column headers as indexes, or another way of doing this, I'd greatly appreciate some guidance.

Thanks!


Gyclone

Using Visual Studio 2008 Standard, VB code behind, ASP.net 2.0, .Net 3.5
0
gyclone
9/30/2008 6:51:49 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
897 Views

Similar Articles

[PageSpeed] 53

Sure, one way would be to create an Enum of your Columns and then override the default values with your column index. Then, you can simply refer to the Enum item and cast it to your column index.

public enum Columns { Color = 4, Year = 5 };

if (e.Row.Cells((int)Columns.Color).Text == "Moss")


Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
9/30/2008 7:07:59 PM

 Thanks for the reply!

I'm assuming your code is C#; my project is VB so I'm trying to adapt.

I have this for the enum section:

Public Enum Columns As Integer
        Color = 4
        Year = 5
 End Enum

I don't get any errors there, but I can't seem to make the other line of code work.  Do you happen to know the VB translation?

 

Thanks again!


Gyclone

Using Visual Studio 2008 Standard, VB code behind, ASP.net 2.0, .Net 3.5
0
gyclone
9/30/2008 7:27:47 PM

Try this: 

If e.Row.Cells(CType(Columns.Color, Integer)).Text = "Moss" Then   'If cell in "color" column reads "Moss"
	If e.Row.Cells(CType(Columns.Year, Integer)).Text < "2005" Then	' and cell in "year" column reads < "2005"
		e.Row.Cells(CType(Columns.Color, Integer)).Text = "Coral"	  ' then change color to "Coral"
	End If
End If

Thanks, Ed

Microsoft MVP - ASP/ASP.NET

0
ecbruck
9/30/2008 7:37:53 PM

 Perfect!

Thanks again!!!


Gyclone

Using Visual Studio 2008 Standard, VB code behind, ASP.net 2.0, .Net 3.5
0
gyclone
9/30/2008 7:41:30 PM
Reply:

Similar Artilces:

Using Bound Controls in Gridview Column Header
Hi Folks, Please tell me how do I use bound controls in Gridview Column Header. Lets say, I want to see the User Name as the Header Text for a Gridview Column.Red in Heart... Hi, I think, If you want to bind any field in your database to the Header of the Column in the GridView control. Then you can add a TemplateField in the gridview control and then add a Textbox or Label field in the HeaderTemplate and then bind it to the field in your Database table. Regards, Manish www.ComponentOne.com    Manish Singhalwww.ComponentOne.com Try this,  <asp:TemplateField> ...

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...

Freezing the column header row of a GridView control using CSS
I am struggling getting my Gridview in ASP.NET 2.0 Visual Studio 2005 to freeze the column header row and provide for scrolling of the remaining data rows. My specific problems right now are: (IE7) column headers freeze as intended and do not scroll  BUT scroll bar is not initially visible to user (requires scrolling with mouse wheel before scroll bars are exposed in gridview) (Firefox 2.0.10) column header row just scrolls out of sight like any other row  Here is part of my default.css in my default App_Theme folder: /* So the overflow scrolls */ div.coveragesContainer ...

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...

Index of column by the Name of Column in GridView
Hi.How can I get the Index of column in GridView if column name is known? Hi, Use following function  private int GetColumnIndex(string columnName, GridView gridView) { int i = 0; foreach (DataControlField dc in gridView.Columns) { if (dc.HeaderText == columnName) { return i; } i++; } return -1; } Please mark post as answer if it helped youMy Blog...

Ajax Gridview drag column header to reorder column
I want to dynamically(Client side) reorder column of my gridview  by drag that column on desired positionand at the same time column like check box temp let column at fix position on the first columnPlease help.....   Thanks in advance  Hi, You got any solutions for this, i do have the same requirement for column sequecing on client side for my asp.net gridview. Please share the same if you have any solutions.  Thanks, Mohan D...

How to reference Column in GridView without specifying column number
How to reference Columns in a GridView without specifying column number? If I want to change a property of a column at run-time I have to use the column number for example: GridView1.Columns[5].SomeProperty = xyz Now if in the future anyone adds or removes a column before this column then Column # 5 will not be what I expect (it will shift to another number!). This is a serious risk! Is there some elegant way of identifying columns with an ID that is not dependent on the position of the column??? Thanks for you help! David I agree with you.  These function may help. ...

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...

remove a gridview column dynamically, removes controls in template columns
I have a interface with a treeview and a gridview. The gridview has two templatefields that handles insert/update/deletes. The gridview is handling editing of different tables so I create the other columns in the gridview dynamically as boundfields. The user selects the table to edit in the treeview. The problem I am having happens when the user selects a table to edit the second time. The code that executes removes all bound fields, (dynamically added when user selected the first table in the treeview), but not the two template fields, and then adds boundfields specifi...

Cannot delete columns of GridView by using "Edit Columns..."
Hi all, I have a GridView with quite a few auto-generated fields. When I go to GridView Tasks to use "Edit Columns..." to delete a few columns, the design window shows the columns removed just as expected. However, the source code does not change at all. If I save the .aspx file and reopen it, it shows all the deleted columns like nothing has changed.  I can go to the source code to delete the fields and the deletion will stay.  I am using VS 2008 standard edition. Could anyone shed some light on this?  Hong Hi,In the gridview have some Bound fields from edit co...

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...

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....

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...

Freeze Column Headers in gridview controls?
Is there a way to freeze the column header row in a gridview control?I am displaying a large table, so the users have to scroll sideways and up and down. I tried to improve this by putting the gridview in a panel, so they can scroll using the panel's scroll buttons and I am able to specify the size of the display. However, if the users scroll down, they loose the name of the columns. Orlando. Here is a way: http://web.tampabay.rr.com/bmerkey/examples/locked-column-csv.htmlSteve Wellens My blog Thank you for that information. It's what I need, but I need to apply this to a gridview, ...

Web resources about - Reference gridview column index using column headers - asp.net.presentation-controls

European Terrestrial Reference System 1989 - Wikipedia, the free encyclopedia
The European Terrestrial Reference System 1989 ( ETRS89 ) is a ECEF (Earth-Centered, Earth-Fixed) geodetic Cartesian reference frame , in which ...

New York Times Removes Reference to President Obama and Cable News
... — Noah Rothman (@NoahCRothman) December 18, 2015 The Times story was published Thursday, but by Friday morning, the reference to cable news ...

Reference Pricing tweak for Medicaid
Reference pricing is a common payment reform for commercial insurance. Common, non-urgent, deferrable procedures are prime candidates. Hip replacement ...

New Apple TV ads are clever reference to Six Colors design - Business Insider Deutschland
The new Apple TV ads are a reference to a crucial time in Apple history.

Donald Trump prompts Nazi references
Some conservatives and political commentators say the GOP frontrunner's rhetoric has crossed the line

Hillary Clinton Ends Democratic Debate With A Star Wars Reference
The other day, President Obama ended his White House press conference with a reference to The [...]

Review: Klipsch X20i Reference In-Ear Headphones
These Klipsch X20i Reference In-Ear Headphones are pretty darn fantastic, and worth every penny.`

Kentucky Grade School Scrubs All References To Christianity In ‘Charlie Brown Christmas’
Kentucky Grade School Scrubs All References To Christianity In ‘Charlie Brown Christmas’

Disney Inundates General Hospital Episode With References To New Year's Eve College Football Playoff ...
ESPN says it isn’t worried about viewership for tomorrow’s college football playoff games on New Year’s Eve , but the degree to which TV viewers ...

Now you can bookmark Google image searches for later reference
Normally, image searches on Google are ephemeral things and gone the moment you close the browser tab. However, a new feature on the search giant's ...

Resources last updated: 1/2/2016 4:58:05 PM