On my page, there are two gridviews where data are added to them dynamically using code behind. There are no problems with the code as far as I can see, the first gridview runs fine, but as soon as I want to add data to the second it gives me the error of:
Column 'LabourType' does not belong to table Table1
This is the error it gives when I initialize the labour grid first (Everything goes through fine). However, if I initialize the PartsGrid first it gives me the same error, just with the first column name of the parts grid.
This is what I have done in the 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;
}
Can anybody please tell me if I am doing anything wrong? Or how can I solve this to allow both grids for data to be added dynamically? Thanks in advance
EDIT - More Data
For the Parts Grid this is how I create and add data to the datatable:
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();
}
And for the labour:
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();
}
This is how my gridviews are created in .aspx Parts:
<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>
Labour:
<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>
I'm binding the grids on the button click events (modal popop) as follows:
protected void btnSubmitPart_Click(object sender, EventArgs e)
{
AddToDataTableParts();
BindGridParts();
ClearPartsToAddNewItem();
}
protected void btnSubmitLabour_Click(object sender, EventArgs e)
{
AddToDataTableLabour();
BindGridLabour();
ClearLabourToAddNewItem();
}