我有一个显示所有行程的gridview - 在该gridview中我有一个转发器,显示在该行程中访问过的国家(一对多)。我的转发器假设要绘制tripID然后填充数据源,该数据源填充转发器。不幸的是,我的转发器正在填充指定的最后一个tripID。
<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>
我的代码背后
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();
}
答案 0 :(得分:0)
项目数据绑定重新绑定对我来说是错误的。它将在每一行重新绑定,这可能解释了为什么你只得到第一行。我认为你应该做的就是彻底删除所有并将RowDataBound1更改为:
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();
}
}
希望这有帮助。
答案 1 :(得分:0)
我决定不使用DataSource并使用我自己的,在移动到下一行之前,我给了我更多的灵活性来绑定gridview中的转发器(不需要gridview)。