• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69241
  • Last Modified:

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

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

        dt.Rows.Add(drow)

        Sessions.SelectedDataTable = dt

    End Sub

Can anyone help me with this

0
de1209
Asked:
de1209
1 Solution
 
Dnx_7Commented:
can u give us the value of :

gvSelectAddress.SelectedRow.Cells(2).Text
0
 
DreamMasterCommented:
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..

Regards,
Max.
0
 
de1209Author Commented:
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.
0
 
DreamMasterCommented:
No problem de1209..

Regards,
Max.
0
 
DarthModCommented:
PAQed with points refunded (500)

DarthMod
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now