Hi Guys,

I've got a code that basically storing data from several controls, one of the control is GridView. when I "Submit" I get all rows from GridView and I need only the selected rows..

I've tried to use: str = GridView1.SelectedRow.Cells(1).Text
but it gives me an error when I run the application.

Can you help please? Thanks!

Protected Sub SaveOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveOrder.Click

        Dim str As String
        For i As Integer = 0 To GridView1.Rows.Count - 1
            Dim row As GridViewRow = GridView1.Rows(0)
            Dim isChecked As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
            If isChecked Then
                str = GridView1.Rows(i).Cells(1).Text
            End If

            Dim strUID As String
            strUID = Session.Item("uid").ToString
            Dim cmd As New SqlCommand("INSERT INTO fo_reports (order_date,code,dept_id,id_number,sug,uid)VALUES('" & Calendar1.SelectedDate & "','" & DropDownList1.SelectedItem.Text & "','" & DropDownList2.SelectedItem.Text & "','" & str.ToString & "','" & DropDownList3.SelectedItem.Text & "','" & strUID & "')", New SqlConnection(strConn))
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
        Next

        Response.Redirect("order.aspx")


    End Sub

You can add for each field you need a textbox in the same form with the same bindingsource as the GridView (when you are working with a database). The textboxes will have the same text as each cell with the same column in the dataridview.

It is not with a code, but it works

dim string1 as string

string1 = gridview1.rows(row#).cells(cell#).text.tostring
commented: thanks...its worked to me +1

Thanks Jx_Man but it is the same what I am doing today, I need to get the data from selected rows only to a string each time.

For example I have a list of people and the user selects 3 of the checkboxes, how do I store those three names for use for individual string and not all of them together (which is what I am doing today)...

Thanks a lot..

Ok, I found the final solution... here is the code...

Protected Sub SaveOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveOrder.Click
        Dim str As String
        Dim row As GridViewRow
        Dim index As Integer = GridView1.SelectedIndex
        For Each row In GridView1.Rows
            If index = -1 Then
                index = 0
            End If
            Dim RowCheckBox As CheckBox = CType(GridView1.Rows(index).FindControl("CheckBox1"), CheckBox)
            If RowCheckBox.Checked Then
                str = GridView1.Rows(index).Cells(1).Text

                ' prints out the result
                Dim strUID As String
                strUID = Session.Item("uid").ToString
                Dim cmd As New SqlCommand("INSERT INTO fo_reports (order_date,code,dept_id,id_number,sug,uid)VALUES('" & Calendar1.SelectedDate & "','" & DropDownList1.SelectedItem.Text & "','" & DropDownList2.SelectedItem.Text & "','" & str & "','" & DropDownList3.SelectedItem.Text & "','" & strUID & "')", New SqlConnection(strConn))
                cmd.Connection.Open()
                cmd.ExecuteNonQuery()
                cmd.Connection.Close()
            End If
            index += 1
        Next

        Response.Redirect("order.aspx")

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.