ASP.NET嵌套数据网格-在所选数据源错误中找不到字段或属性

本文关键字:错误 数据源 找不到 字段 属性 数据 嵌套 NET 数据网 网格 ASP | 更新日期: 2023-09-27 18:28:29

我有一个嵌套的数据网格,代码如下:

<asp:DataGrid ID="QuotesGrid" Runat="server" AutoGenerateColumns="False" PageSize="1"     AllowPaging="True">
<Columns>
    <asp:BoundColumn DataField="QuoteNum" SortExpression="QuoteNum" HeaderText="Quote Number">
    </asp:BoundColumn>
    <asp:BoundColumn DataField="TerritoryTerritoryDesc" SortExpression="Territory" HeaderText="Sales Territory">
    </asp:BoundColumn>
    <asp:BoundColumn DataField="DueDate" SortExpression="DueDate" HeaderText="Due Date">
    </asp:BoundColumn>
    <asp:BoundColumn DataField="BTCustID" SortExpression="BTCustID" HeaderText="Customer ID">
    </asp:BoundColumn>
    <asp:BoundColumn DataField="CustomerName" SortExpression="CustomerName" HeaderText="Customer Name">
    </asp:BoundColumn>
    <asp:BoundColumn DataField="QuoteAmt" SortExpression="QuoteAmt" HeaderText="Total Quote Value">
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Quote Lines">
    <ItemTemplate>
    <asp:DataGrid ID="QuoteLines_Grid" Runat="server" AllowPaging="False" AllowSorting="False" AutoGenerateColumns="False"
            ShowHeader="False">         
        <Columns>                   
                <asp:BoundColumn DataField="QuoteLine" HeaderText="Line Number"></asp:BoundColumn>
        <asp:BoundColumn DataField="PartNum" HeaderText="Part Number"></asp:BoundColumn>
                <asp:BoundColumn DataField="LineDesc" HeaderText="Line Description"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    </ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages">
</PagerStyle>
</asp:DataGrid>     

我的代码如下:

DataSet dsQuote = new DataSet("dsQuote");
DataTable dtQuoteHed = new DataTable("dtQuoteHed");
dtQuoteHed.Columns.Add("QuoteNum", typeof(int));
dtQuoteHed.Columns.Add("QuoteLine", typeof(int)); 
dtQuoteHed.Columns.Add("TerritoryTerritoryDesc", typeof(string));
dtQuoteHed.Columns.Add("DueDate", typeof(DateTime));
dtQuoteHed.Columns.Add("BTCustID", typeof(string));
dtQuoteHed.Columns.Add("CustomerName", typeof(string));            dtQuoteHed.Columns.Add("QuoteAmt", typeof(decimal));
dtQuoteHed.PrimaryKey = new DataColumn[] { dtQuoteHed.Columns["QuoteNum"] };
DataTable dtQuoteDtl = new DataTable("dtQuoteDtl");            
dtQuoteDtl.Columns.Add("QuoteNum", typeof(int));
dtQuoteDtl.Columns.Add("QuoteLine", typeof(int));
dtQuoteDtl.Columns.Add("PartNum", typeof(string));
dtQuoteDtl.Columns.Add("LineDesc", typeof(string));
dtQuoteDtl.PrimaryKey = new DataColumn[] { dtQuoteDtl.Columns["QuoteNum"], dtQuoteDtl.Columns["QuoteLine"] };
dsQuote.Tables.AddRange(new DataTable[] { dtQuoteHed, dtQuoteDtl });
dsQuote.Relations.Add(new DataRelation("relQuoteHedtoQuoteDtl", dtQuoteHed.Columns["QuoteNum"], dtQuoteDtl.Columns["QuoteNum"]));
//FILL THE DATASET - ROWS HAVE BEEN VALIDATED
DataGrid DataG = (DataGrid)this.FindControl("QuotesGrid");
DataG.ItemDataBound += new DataGridItemEventHandler(this.OnMainGridDataBound);
DataG.DataSource = dsQuote.Tables["dtQuoteHed"];
dsQuote.WriteXmlSchema("C:''test.xml");
DataG.DataBind();  
//EVENT HANDLER
private void OnMainGridDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        DataGrid obCtl = (DataGrid)e.Item.FindControl("QuoteLines_Grid");
        if (null != obCtl && obCtl is DataGrid)
        {                                    
            obCtl.DataSource = dsQuote.Tables["dtQuoteDtl"].Select("QuoteNum=" + ((DataRowView)e.Item.DataItem).Row["QuoteNum"]);
            obCtl.DataMember = "dtQuoteDtl";
            obCtl.DataBind();  // THIS IS WHERE I GET THE ERROR! - COLUMN EXISTS!!
        }
    }
}

//这是xmlatr

<?xml version="1.0" standalone="yes"?>
<xs:schema id="dsQuote" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="dsQuote" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="dtQuoteHed">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="QuoteNum" type="xs:int" />
              <xs:element name="TerritoryTerritoryDesc" type="xs:string" minOccurs="0" />
              <xs:element name="DueDate" type="xs:dateTime" minOccurs="0" />
              <xs:element name="BTCustID" type="xs:string" minOccurs="0" />
              <xs:element name="CustomerName" type="xs:string" minOccurs="0" />
              <xs:element name="QuoteAmt" type="xs:decimal" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="dtQuoteDtl">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="QuoteNum" type="xs:int" />
              <xs:element name="QuoteLine" type="xs:int" />
              <xs:element name="PartNum" type="xs:string" minOccurs="0" />
              <xs:element name="LineDesc" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//dtQuoteHed" />
      <xs:field xpath="QuoteNum" />
    </xs:unique>
    <xs:unique name="dtQuoteDtl_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//dtQuoteDtl" />
      <xs:field xpath="QuoteNum" />
      <xs:field xpath="QuoteLine" />
    </xs:unique>
    <xs:keyref name="relQuoteHedtoQuoteDtl" refer="Constraint1">
      <xs:selector xpath=".//dtQuoteDtl" />
      <xs:field xpath="QuoteNum" />
    </xs:keyref>
  </xs:element>
</xs:schema>

在所选数据源上找不到名为"QuoteLine"的字段或属性。此处的图像链接:数据源错误

但我已经证实了这个领域的存在。

此处的图像链接:列调试

正如你所看到的,我甚至在数据表dtQuoteHed中添加了一个"QuoteLine"字段,但这没有任何区别。。。

有人知道我哪里错了吗?

非常感谢!

ASP.NET嵌套数据网格-在所选数据源错误中找不到字段或属性

看看这个部分:

DataTable dtQuoteHed = new DataTable("dtQuoteHed");
dtQuoteHed.Columns.Add("QuoteNum", typeof(int));
dtQuoteHed.Columns.Add("QuoteLine", typeof(int)); 

您正在为dtQuoteHed表定义Quoteline列,但在架构中dtQuoteHed元素没有Quoteline子元素。因此,将其从dtQuoteHed数据表中删除:

DataSet dsQuote = new DataSet("dsQuote");
DataTable dtQuoteHed = new DataTable("dtQuoteHed");
dtQuoteHed.Columns.Add("QuoteNum", typeof(int));
dtQuoteHed.Columns.Add("TerritoryTerritoryDesc", typeof(string));
dtQuoteHed.Columns.Add("DueDate", typeof(DateTime));
dtQuoteHed.Columns.Add("BTCustID", typeof(string));
dtQuoteHed.Columns.Add("CustomerName", typeof(string));      
dtQuoteHed.Columns.Add("QuoteAmt", typeof(decimal));
dtQuoteHed.PrimaryKey = new DataColumn[] { dtQuoteHed.Columns["QuoteNum"] };