Solved

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

Posted on 2011-09-13
6
211 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
Industry Leaders: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
start a process from a service 3 36
Help with preventing selection from a combobox 11 39
Open a link in vb.net 2 25
System.net.Sockets Error 5 38
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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