Solved

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

Posted on 2011-09-13
6
200 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
  • 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
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.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now