创建错误

时间:2016-02-11 10:07:35

标签: c# asp.net gridview

在我的页面上,有两个网格视图,其中使用后面的代码动态地向其添加数据。据我所知,代码没有问题,第一个gridview运行正常,但只要我想将数据添加到第二个,它就会给出错误:

    Column 'LabourType' does not belong to table Table1

这是我首先初始化劳动网格时出现的错误(一切都很顺利)。但是,如果我首先初始化PartsGrid,它会给出同样的错误,只是使用零件网格的第一个列名称。

这就是我在Page_Load中所做的:

    DataTable dtParts;
    DataTable dtLabour;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateCustomers();

            dtLabour = new DataTable();
            MakeDataTableLabour();
            EmptyDataStringLabour();
            this.gvLabour.EditIndex = -1;

            dtParts = new DataTable();
            MakeDataTableParts();
            EmptyDataStringParts();
            this.gvParts.EditIndex = -1;
        }
        else
        {
            dtLabour = (DataTable)ViewState["DataTable"];
            dtParts = (DataTable)ViewState["DataTable"];
        }

        ViewState["DataTable"] = dtLabour;
        ViewState["DataTable"] = dtParts;
    }

任何人都可以告诉我,如果我做错了吗?或者我该如何解决这个问题以允许动态添加两个数据网格?提前致谢

编辑 - 更多数据

对于Parts Grid,这是我创建数据并向数据表添加数据的方式:

    private void MakeDataTableParts()
    {
        dtParts.Columns.Add("Units");
        dtParts.Columns.Add("PartNo");
        dtParts.Columns.Add("Description");
        dtParts.Columns.Add("CostPrice");
    }

    private void AddToDataTableParts()
    {
        DataRow drParts = dtParts.NewRow();
        drParts["Units"] = txtUnits.Text.Trim();
        drParts["PartNo"] = txtPartNo.Text.Trim();
        drParts["Description"] = txtDescription.Text.Trim();
        drParts["CostPrice"] = txtCostPrice.Text.Trim();
        dtParts.Rows.Add(drParts);
    }

     private void BindGridParts()
    {
        gvParts.DataSource = dtParts;
        gvParts.DataBind();
    }

对于劳动力:

    private void MakeDataTableLabour()
    {
        dtLabour.Columns.Add("Units");
        dtLabour.Columns.Add("LabourType");
        dtLabour.Columns.Add("LabourCost");
    }

    private void AddToDataTableLabour()
    {
        DataRow drLabour = dtLabour.NewRow();
        drLabour["Units"] = txtLabourUnits.Text;
        drLabour["LabourType"] = lstLabourType.SelectedValue.ToString();
        drLabour["LabourCost"] = txtLabourCost.Text;
        dtLabour.Rows.Add(drLabour);
    }

     private void BindGridLabour()
    {
        gvLabour.DataSource = dtLabour;
        gvLabour.DataBind();
    }

这就是我在.aspx中创建网格视图的方式 部分:

    <asp:GridView ID="gvParts" runat="server" AutoGenerateColumns="true" CellPadding="8" CellSpacing="8" AllowPaging="false"
                                 Width="100%" OnPageIndexChanging="gvParts_PageIndexChanging" OnRowCommand="gvParts_RowCommand" 
                                     OnRowCancelingEdit="gvParts_RowCancelingEdit" OnRowEditing="gvParts_RowEditing" 
                                     OnRowUpdating="gvParts_RowUpdating" OnRowDeleting="gvParts_RowDeleting"  >
                                <emptydatatemplate>
                                    There are currently no parts listed.
                                </emptydatatemplate> 
                            <Columns>
                                <asp:TemplateField HeaderText=""> 
                                    <ItemTemplate> 
                                        <asp:LinkButton ID="lnkEditParts" runat="server"CommandName="EditParts" CommandArgument='<%# Container.DataItemIndex %>'>Edit</asp:LinkButton>
                                    </ItemTemplate> 
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText=""> 
                                    <ItemTemplate> 
                                        <asp:LinkButton ID="lnkRemoveParts" runat="server" CommandName="DeleteParts" CommandArgument='<%# Container.DataItemIndex %>'>Remove</asp:LinkButton>
                                    </ItemTemplate> 
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle CssClass="Grid" />
                            <RowStyle CssClass="rowStyle" />
                            <AlternatingRowStyle CssClass="alt" />
                            <PagerStyle CssClass="pgr" />
                        </asp:GridView>

人工:

    <asp:GridView ID="gvLabour" runat="server" AutoGenerateColumns="true" CellPadding="8" CellSpacing="8" AllowPaging="false"
                             Width="100%" OnPageIndexChanging="gvLabour_PageIndexChanging" OnRowCommand="gvLabour_RowCommand" 
                                 OnRowCancelingEdit="gvLabour_RowCancelingEdit" OnRowEditing="gvLabour_RowEditing" 
                                 OnRowUpdating="gvLabour_RowUpdating" OnRowDeleting="gvLabour_RowDeleting"  >
                            <emptydatatemplate>
                                There are currently no labour listed.
                            </emptydatatemplate> 
                            <Columns>
                                <asp:TemplateField HeaderText=""> 
                                    <ItemTemplate> 
                                        <asp:LinkButton ID="lnkEditLabour" runat="server"CommandName="EditLabour" CommandArgument='<%# Container.DataItemIndex %>'>Edit</asp:LinkButton>
                                    </ItemTemplate> 
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText=""> 
                                    <ItemTemplate> 
                                        <asp:LinkButton ID="lnkRemoveLabour" runat="server" CommandName="DeleteLabour" CommandArgument='<%# Container.DataItemIndex %>'>Remove</asp:LinkButton>
                                    </ItemTemplate> 
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle CssClass="Grid" />
                            <RowStyle CssClass="rowStyle" />
                            <AlternatingRowStyle CssClass="alt" />
                            <PagerStyle CssClass="pgr" />
                        </asp:GridView>

我在按钮点击事件(模式popop)上绑定网格,如下所示:

    protected void btnSubmitPart_Click(object sender, EventArgs e)
    {
        AddToDataTableParts();
        BindGridParts();
        ClearPartsToAddNewItem();
    }

    protected void btnSubmitLabour_Click(object sender, EventArgs e)
    {
        AddToDataTableLabour();
        BindGridLabour();
        ClearLabourToAddNewItem();
    }

1 个答案:

答案 0 :(得分:1)

错误是自我解释的。您已将列LabourType设置为DataField,或在GridView之一的数据表达式中使用此列,但此列不在DataSource中。