Solved

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

Posted on 2011-09-13
6
188 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

20 Experts available now in Live!

Get 1:1 Help Now