嵌套GridView创建一个额外的列

时间:2012-01-27 01:38:27

标签: c# asp.net gridview

我有一个嵌套的gridview,由于某种原因正在创建一个重复的列。列中的数据是正确的,但有两列而不是一列。我无法摆脱它,我不知道它为什么创建重复列。 HTML标记只显示一个网格,但只显示两个网格。我还确保数据绑定只发生一次,而且确实如此。有什么想法吗?

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" CSSClass="viewSourceGrid" ShowHeader="false">
                        <Columns>
                            <asp:BoundField DataField="dateCode" HeaderText="DateCode" ReadOnly="true" />
                        </Columns>
                    </asp:GridView>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>
</div>

aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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();
        GridViewRow row = e.Row;
        //int sourceLineItem = int.Parse(Source.DataKeys[row.RowIndex].Value.ToString());

        if (row.RowType == DataControlRowType.DataRow)
        {
            Response.Write("One <br>");
            // Find the nested grid view
            GridView nested = (GridView)row.FindControl("DateCodeGrid");
            //Figure out how to get the sourceLineItem
            DataSet dateCodes = db.GetDateCodes(71);
            nested.DataSource = dateCodes;
            nested.DataBind();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在嵌套网格视图中设置AutoGenerateColumns =“False”