Solved

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

Posted on 2011-09-13
6
214 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

752 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