我正在尝试将一个gridview嵌套在另一个gridview中,但我无法在第二个网格视图中填充数据。我在尝试设置第二个数据网格的数据源时遇到错误(说它为空)。有人可以帮忙吗? 这是aspx页面:
<div id="divSource" runat="server" align="center">
<asp:GridView ID="Source" runat="server" AutoGenerateColumns="False" DataKeyNames="sourceLineItem" CSSClass="viewSourceGrid" OnRowDataBound="PopulateDateCodes">
<Columns>
<asp:TemplateField InsertVisible="False" HeaderStyle-Width="70px">
<ItemTemplate>
<asp:Label CssClass="sourceHeader" runat="server" Text= '<%# "Source: " + (Container.DataItemIndex + 1).ToString() %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="nfdBroker" HeaderText="NFD/Broker" InsertVisible="False" ReadOnly="True" SortExpression="nfdBroker" />
<asp:BoundField DataField="locationDescription" HeaderText="Material Location" SortExpression="materialLocation" />
<asp:BoundField DataField="origPkg" HeaderText="Original Packaging?" SortExpression="origPkg" />
<asp:BoundField DataField="oemCC" HeaderText="OEM C of C? " InsertVisible="False" ReadOnly="True" SortExpression="oemCC" />
<asp:BoundField DataField="minBuyQty" HeaderText="Minimum Buy Qty" SortExpression="minBuyQty" />
<asp:BoundField DataField="deliveryInfo" HeaderText="Delivery" SortExpression="delUOM" />
<asp:TemplateField InsertVisible="False" HeaderText="Date Codes" >
<ItemTemplate>
<asp:GridView ID="DateCodeGrid" runat="server" InsertVisible="False" DataKeyNames="dateCode" CSSClass="viewSourceGrid" >
<Columns>
<asp:BoundField DataField="dateCode" SortExpression="dateCode">
<ItemStyle Width="20%" />
</asp:BoundField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然后是后面的代码:
public partial class Controls_ViewSource : System.Web.UI.UserControl
{
//Set the Source Line Item
public int SourceLineItem { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.SourceLineItem = SourceLineItem;
RequestDB db = new RequestDB();
DataSet sources = db.GetSource(int.Parse(Request.QueryString["requestNumber"]), SourceLineItem);
Source.DataSource = sources;
Source.DataBind();
}
protected void PopulateDateCodes(object sender, GridViewRowEventArgs e)
{
RequestDB db = new RequestDB();
int index = e.Row.RowIndex;
GridView gv = (GridView)Source.Rows[0].FindControl("DateCodeGrid");
//int sourceLineItem = int.Parse(Source.DataKeyNames[0].ToString());
//Response.Write(Source.DataKeyNames[0].ToString());
DataSet dateCodes = db.GetDateCodes(71);
gv.DataSource = dateCodes;
gv.DataBind();
}
}
答案 0 :(得分:3)
您需要在数据绑定的行中找到嵌套的网格视图:
GridViewRow row = e.Row;
您需要确保只对数据行执行此操作,而不是页眉或页脚行:
if(row.RowType == DataControlRowType.DataRow)
{
// Find the nested grid view
GridView nested = (GridView)row.FindControl("DateCodeGrid");
// The rest of your code for binding the nested grid view follows here
}