views:

36

answers:

2

I have a gridview that displays all trips - within that gridview I have a repeater that displays the countries visited during that trip (one to many). My repeater is suppose to grap the tripID and then populate the datasource, which populates the repeater. Unfortunately, my repeater is just being populated with the last tripID specified.

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="pkiTripID"
                    DataSourceID="SqlDataSource2" PageSize="20" AllowPaging="True" 
                    Width="100%" AllowSorting="True"
                    GridLines="None" onrowdatabound="GridView1_RowDataBound1">                 
                    <Columns>
                        <asp:BoundField DataField="TripType1" HeaderText="Type" SortExpression="TripType1" />
                        <asp:BoundField DataField="RegionName" HeaderText="Region" SortExpression="RegionName" />
                        <asp:TemplateField HeaderText="Country" HeaderStyle-ForeColor="#FF7900">
                            <ItemTemplate>
                                <asp:Repeater ID="RepeatCountry" runat="server"  
                                    DataSourceID="dsCountryByTripID" onitemdatabound="RepeatCountry_ItemDataBound">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "CountryName") %>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </ItemTemplate>
                            <HeaderStyle ForeColor="#FF7900" />
                        </asp:TemplateField>
                        <asp:BoundField DataField="OverallRating" HeaderText="Trip Rating" 
                            SortExpression="OverallRating" />
                        <asp:BoundField DataField="DepartureDate" HeaderText="Date" SortExpression="DepartureDate"
                            DataFormatString="{0:MMM-dd-yyyy}" HtmlEncode="false" />
                    </Columns>
                </asp:GridView> 

 <asp:SqlDataSource ID="dsCountryByTripID" runat="server" 
        ConnectionString="<%$ ConnectionStrings:myDB %>" 
        SelectCommand="spSelectCountriesByTripID" SelectCommandType="StoredProcedure">
        <SelectParameters>
        <asp:Parameter Type="Int32" Name="tripID"  DefaultValue="96" />
        </SelectParameters>
    </asp:SqlDataSource>

And my code behind

  protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        Repeater rp = (Repeater)e.Row.FindControl("RepeatCountry");

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            dsCountryByTripID.SelectParameters.Clear();
            DataRowView drv = (DataRowView)e.Row.DataItem;
            string tripID = (drv["pkiTripId"]).ToString();
            dsCountryByTripID.SelectParameters.Clear();
            dsCountryByTripID.SelectParameters.Add("tripID", DbType.Int32, tripID);
            ////gv2.DataBind();
            //e.Row.DataBind();

        }
    }
    protected void RepeatCountry_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        e.Item.DataBind();
    }
A: 

The item data Bound rebind looks wrong to me. It will be rebinding on every row which probably explains why you only get the first row. What I think you should do is remove that alltogether and change the RowDataBound1 to this:

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    Repeater rp = (Repeater)e.Row.FindControl("RepeatCountry");

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        dsCountryByTripID.SelectParameters.Clear();
        DataRowView drv = (DataRowView)e.Row.DataItem;
        string tripID = (drv["pkiTripId"]).ToString();
        dsCountryByTripID.SelectParameters.Clear();
        dsCountryByTripID.SelectParameters.Add("tripID", DbType.Int32, tripID);

        rp.DataBind();

    }
}

Hope this helps.

madcapnmckay
try that, didn't seem to work :(
Spooks
A: 

I decided not to use DataSource and use my own, I gave me more flexibility to bind the repeater(no need for gridview) within the gridview, before moving to the next row.

Spooks