Hereok
Hereok

Reputation: 41

RowDataBound not showing all rows in GridView

I have a RowDataBound that loops each record and executes a query with the OrderId to get the detail data. Then, its values are bound to a nested GridView named gvOrders. The issue I'm having is that I'm only getting the results of the last OrderId value of the loop bound to the GridView gvOrders. Can anyone help me get the results of all the loops in the GridView gvOrders? There seems to be an issue with my binding method.

Protected Sub GridViewDetailInfo_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridViewDetailInfo.RowDataBound

    Dim strSQL As String

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim OrderId As String = e.Row.Cells(1).Text
        Dim gvOrders As GridView = DirectCast(e.Row.FindControl("gvOrders"), GridView)

        strSQL = "select Qty, Prod, Date from Order..orders where OrderNo='" + OrderId + "'"

        Me.SqlDataSourceDetail_Child.SelectCommand = strSQL
        gvOrders.DataBind()

    End If

End Sub

Upvotes: 0

Views: 514

Answers (1)

j.f.
j.f.

Reputation: 3939

You are using the same SqlDataSource (SqlDataSourceDetail_Child) for all of your child GridViews. So when it finally gets to the last OrderId, you set the SelectCommand of the shared SqlDataSource and now all of the child GridViews get the same, shared set of data.

To get around that, each child GridView will need its own data source. You could continue the way you are and create a separate SqlDataSource for each of the child GridViews, but I'm guessing the amount of child GridViews is not constant. In that case, you will need to get rid of the child SqlDataSource and bind a data source such as a DataTable to each child GridView.

This should at least point you in the right direction:

Protected Sub GridViewDetailInfo_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridViewDetailInfo.RowDataBound

    Dim strSQL As String

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim OrderId As String = e.Row.Cells(1).Text
        Dim gvOrders As GridView = DirectCast(e.Row.FindControl("gvOrders"), GridView)

        strSQL = "select Qty, Prod, Date from Order..orders where OrderNo='" + OrderId + "'"

        gvOrders.DataSource = FunctionToRetrieveData(strSQL)
        gvOrders.DataBind()

    End If

End Sub

Also note that you are open to SQL injection. I suggest looking into parameterized queries.

Upvotes: 1

Related Questions