为什么选择DataBinding:' System.Data.DataRowView'不包含名称为' xxx&#39 ;?的属性

时间:2017-09-18 06:53:01

标签: c# asp.net c#-4.0 gridview oledb

我正在从Excel工作表导入数据,但它适用于一个工作表,但不适用于具有相同列和正确数据的另一个工作表。它适用于许多文件但不是很少。我已经将代码附加了下面的示例和错误。在绑定时,它会抛出错误。

正在运作的工作表:

enter image description here

和导致错误的工作表:

enter image description here

错误:

DataBinding:' System.Data.DataRowView'不包含名称为' Amount'

的媒体资源

代码:

private void ImportDataToGrid(string FilePath, string Extension, string isHDR)
    {
        string conStr = "";
        switch (Extension)
        {
            case ".xls": //Excel 97-03
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                break;
            case ".xlsx": //Excel 07
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                break;
        }
        conStr = String.Format(conStr, FilePath, isHDR);
        OleDbConnection connExcel = new OleDbConnection(conStr);
        OleDbCommand cmdExcel = new OleDbCommand();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        DataTable dt = new DataTable();
        cmdExcel.Connection = connExcel;

        //Get the name of First Sheet
        connExcel.Open();
        DataTable dtExcelSchema;
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
        connExcel.Close();

        //Read Data from First Sheet
        connExcel.Open();
        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
        oda.SelectCommand = cmdExcel;
        oda.Fill(dt);
        connExcel.Close();
        DataColumn dc = new DataColumn("NITNo", typeof(string));
        dc.DefaultValue = txtNitNo.Text.ToString();
        dt.Columns.Add(dc);

        dc = new DataColumn("WorkNo", typeof(string));
        dc.DefaultValue = txtWorkNo.Text.ToString();
        dt.Columns.Add(dc);

        //Bind Data to GridView
        gvBOQ.Caption = Path.GetFileName(FilePath);
        gvBOQ.DataSource = dt;
        gvBOQ.DataBind();
    }

GV:

<asp:GridView ID="gvBOQ" runat="server" AutoGenerateColumns="false" EmptyDataText="No Data Found"
    CssClass="table table-responsive table-bordered table-striped">
    <Columns>
        <asp:TemplateField HeaderText="S.No">
            <ItemTemplate>
                <%# Container.DataItemIndex+1 %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="NIT No">
            <ItemTemplate>
                <asp:Literal ID="liNITNo" runat="server" Text='<%#Eval("NITNo") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Work No">
            <ItemTemplate>
                <asp:Literal ID="liWorkNo" runat="server" Text='<%#Eval("WorkNo") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item Code">
            <ItemTemplate>
                <asp:Literal ID="liItemCode" runat="server" Text='<%#Eval("ItemCode") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item Description">
            <ItemTemplate>
                <asp:Literal ID="liItemDesc" runat="server" Text='<%#Eval("DescriptionOfItem") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:Literal ID="liQuantity" runat="server" Text='<%#Eval("Quantity") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Unit">
            <ItemTemplate>
                <asp:Literal ID="liUnit" runat="server" Text='<%#Eval("Unit") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rate">
            <ItemTemplate>
                <asp:Literal ID="liRate" runat="server" Text='<%#Eval("Rate") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:Literal ID="liAmount" runat="server" Text='<%#Eval("Amount") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

在这些情况下,问题通常是由列名称中有空格()引起的。

我建议检查一下,并删除那里的任何空格。