Solved

datagridview reference values by names instead of numbers in a WebForm?

Posted on 2011-09-13
6
227 Views
Last Modified: 2012-05-12
My displayed datagridview column order is going to be changing around a lot as I work on the webform display.  Is there any way to reference the column values by names instead of numbers?

I have tried several approaches, but they all result in an invalid cast exception.

Thanks in advance!

0
Comment
Question by:codefinger
[X]
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
  • 4
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36532522
Is it not sorted already?
0
 

Author Comment

by:codefinger
ID: 36533024
What an odd question you ask!   Clearly, for you to ask such a question, I have not made MY question very clear.  Let me try again:

I have tried:

str_x = GridViewRegistrants.SelectedRow.Cells("LastName").Text

and

str_x = GridViewRegistrants.Rows(GridViewRegistrants.SelectedIndex).Cells("LastName").Text

But the code above expects an integer where "LastName" is and generates an invalid cast exception when it tries to execute.

I can use GridViewRegistrants.Rows(GridViewRegistrants.SelectedIndex).Cells(int_X).Text  where int_X is an integer, but as I change the order of my columns in the datagrid, the value of int_X will change as well.  So I  am seeking a way to reference the cell values by a non-changing name rather than a frequently-changing number.

Hope that is more clear and I have not muddied the waters even more.

Thanks!

0
 

Accepted Solution

by:
codefinger earned 0 total points
ID: 36536882
Found my own solution (workaround):
 
Public Sub NameColumns()

        Dim cnt As Integer


        Dim str_columnId As String = Nothing

        For cnt = 1 To Me.GridViewRegistrants.Rows(0).Cells.Count - 1

            str_columnId = DirectCast(Me.GridViewRegistrants.Rows(0).Cells(cnt), System.Web.UI.WebControls.DataControlFieldCell).ContainingField.ToString

            Select Case str_columnId.ToUpper

                Case "RECORDID"

                    Me.ColumnsByNumber_RECORDID = cnt

                Case "FIRSTNAME"

                    Me.ColumnsByNumber_FIRSTNAME = cnt

                Case "LASTNAME"

                    Me.ColumnsByNumber_LASTNAME = cnt

                Case "CLINICNO"

                    Me.ColumnsByNumber_CLINICNO = cnt

                Case "ADDRESS1"
                    Me.ColumnsByNumber_ADDRESS1 = cnt

                Case "ADDRESS2"
                    Me.ColumnsByNumber_ADDRESS2 = cnt

                Case "CITY"
                    Me.ColumnsByNumber_CITY = cnt

                Case "STATE"
                    Me.ColumnsByNumber_STATE = cnt

                Case "ZIP"
                    Me.ColumnsByNumber_ZIP = cnt

                Case "PHONE_NUMBER"
                    Me.ColumnsByNumber_PHONE_NUMBER = cnt

                Case "SSN"
                    Me.ColumnsByNumber_SSN = cnt

                Case "EMAIL"
                    Me.ColumnsByNumber_EMAIL = cnt

                Case "GENDER"
                    Me.ColumnsByNumber_GENDER = cnt

                Case "DOB"
                    Me.ColumnsByNumber_DOB = cnt

                Case "STATUS"
                    Me.ColumnsByNumber_STATUS = cnt

                Case "STATUS_UPDATED_ON"
                    Me.ColumnsByNumber_STATUS_UPDATED_ON = cnt

                Case "STATUS_UPDATED_BY"
                    Me.ColumnsByNumber_STATUS_UPDATED_BY = cnt

                

            End Select
        Next

    End Sub

Open in new window


Now I can change column order to my heart's content and NameColumns() will make sure I always refer to the right column when I am try to retrieve the value of a given cell.  (Of course, you should check to see you have at least one row before calling this sub.)

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Closing Comment

by:codefinger
ID: 36558763
Found my own solution.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36539311
The reason for my question, sir, was that a suggestion was made to you in your previous question to which you did not reply.
0
 

Author Comment

by:codefinger
ID: 36540092
The reason for my question, sir, was that a suggestion was made to you in your previous question to which you did not reply.

You are correct and I am embarrassed.   I typed a reply, but until now I did not realize what I typed did not get saved....here is the gist of my answer which I thought had saved on the previous question:

No, neither of those work either...same result.  Technically this should be a separate question anyway, so I am kind of glad we failed to solve it here.  I am going to award the points for the original question here and start a new question.

My apologies for the confusion.

0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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