Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Find gridview column datafield in code.

Posted on 2008-06-26
12
769 Views
Last Modified: 2011-10-03
I am currently building a filtering mechanism for a gridview.  I am binding the gridview to an SQLDataSource.  My user will select a column name from a dropdown list (populated at page load from gridview.columns(i).HeaderText values) and enter a search value in a textbox.  What I want to do at this point is to determine programmatically what data field is associated with the select column HeaderText value.  What I need to locate is the DataField value associated with the column.  Can you help?

Thanks!
0
Comment
Question by:mdunlap99
  • 7
  • 4
12 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 21875595
What do you mean by datafield?  If you want to access the data in the grid for this particular column, you can loop through the number or rows and use this statement (below) that references the current cell of the row.

gridview.rows(#).Cells(i)
0
 

Author Comment

by:mdunlap99
ID: 21875793
I am not looking for the data, just the data field name so that I can construnct a select statement.  For instance, the gridview column header text is 'Product Name'.  The field name in the SQL database if 'prod_name'.  If the user selects 'Product Name' from the dropdown list, I need to change the SQL "select to 'select * where prod_name like ...".  
0
 
LVL 33

Expert Comment

by:raterus
ID: 21875903
Ok I understand.

That depends on your column, but if you are using BoundFields you should be able to use something like this.

Gridview.columns(i).DataField

My hunch though is you're going to tell me this doesn't work, since it seems so obvious :-)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mdunlap99
ID: 21875968
Your hunch is right on.  DataField does not seem to be a property of the Columns(i) item.  Seems there are only headerstyle, Itemstyle, etc. properties there.  Thus the 250 points, eh!!
0
 
LVL 33

Accepted Solution

by:
raterus earned 250 total points
ID: 21876103
Ahh, but have you tried casting the Column to the actual column?

Here's a VB example,

If myGridView.Columns(i) is BoundField Then
  Dim dataField as String = ctype(Gridview.columns(i), BoundField).DataField
End If
0
 

Author Comment

by:mdunlap99
ID: 21876159
Bingo!  That did the trick.  Thanks for the help!!
0
 
LVL 33

Expert Comment

by:raterus
ID: 21876341
Glad you got it to work.  Did you accidently click the wrong link to accept my comment?  I see this notice (which as soon as I post here will go away)

--

Author Comment      
Is this what you were looking for?
Yes No
mdunlap99:
Bingo!  That did the trick.  Thanks for the help!!

Notice: The Asker has requested that this comment be accepted as the solution and the points broken down as follows, with any remaining points refunded to the Asker's account:
250 points to 21876103 by raterus

If you object to this action, you have until 7/3/2008 to post a comment describing your objection. This will generate a request in Community Support for Moderator review. If no Expert objects, this question will be closed as described above.

0
 

Author Comment

by:mdunlap99
ID: 21876363
raterus,
I screwed the point submit.  I am a new member and don't know why this happened.  I will get it fixed, though.
0
 

Author Comment

by:mdunlap99
ID: 21876380
Do you know how I can get this error undone and get the points credited to you?
0
 

Author Comment

by:mdunlap99
ID: 21876414
I see an autorequest in my list to accept and award point.  I hope this clears it up.  
0
 

Author Comment

by:mdunlap99
ID: 21876422
OK, we're flush,eh!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question