How to Get the gridview cell value of the selected row into a datatable

Posted on 2006-05-31
Last Modified: 2011-08-18
I got a gridview in which i selected a row by clicking anywhere on the row and now i am trying to get a cell value of the selected row into a datatable.

I am getting an error saying " Input string was not in a correct format. " at the point given below (at ---->)

Private Sub CreateSelectionAsSessionTable()
        Dim dt As New DataTable
        Dim drow As DataRow

        dt.Columns.Add(New DataColumn("PremiseId", GetType(Integer)))
        dt.Columns.Add(New DataColumn("CustomerId", GetType(Integer)))
        dt.Columns.Add(New DataColumn("Addressline1", GetType(String)))
        dt.Columns.Add(New DataColumn("Name1", GetType(String)))
        dt.Columns.Add(New DataColumn("HomePhoneNumber", GetType(String)))
        dt.Columns.Add(New DataColumn("WorkPhoneNumber", GetType(String)))

        drow = dt.NewRow()

        Dim i As Integer = gvSelectAddress.SelectedIndex

        If i = -1 Then i = 1

   ----->     drow("PremiseId") = CType(gvSelectAddress.SelectedRow.Cells(2).Text, Integer)
        drow("addressline1") = gvSelectAddress.Rows(i).Cells(7).Text
        drow("name1") = gvSelectAddress.Rows(i).Cells(8).Text
        drow("HomePhoneNumber") = gvSelectAddress.Rows(i).Cells(9).Text
        drow("WorkPhoneNumber") = gvSelectAddress.Rows(i).Cells(10).Text


        Sessions.SelectedDataTable = dt

    End Sub

Can anyone help me with this

Question by:de1209
    LVL 2

    Expert Comment

    can u give us the value of :

    LVL 19

    Expert Comment

    drow("PremiseId") = CType(gvSelectAddress.SelectedRow.Cells(2).Text, Integer)

    should be (as far as I know)

    drow("PremiseId") = Convert.ToInt16(gvSelectAddress.SelectedRow.Cells(2).Text)
    drow("PremiseId") = Convert.ToInt32(gvSelectAddress.SelectedRow.Cells(2).Text)
    drow("PremiseId") = Convert.ToInt64(gvSelectAddress.SelectedRow.Cells(2).Text)

    either of these three..


    Author Comment

    I finally could figure out the solution

    Here the premiseId in my gvSelectAddress is hidden .
    I assigned this value to dataKeyNames of the gridview and accessed the value using

    gvSelectAddress.datakey(Index).(datakeyname value position).Tostring

    Anyway Thanks Dnx_7  and DreamMaster  for trying to help me.
    LVL 19

    Expert Comment

    No problem de1209..

    LVL 1

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now