I'm using the below code to add a new row to a ASP.net gridview. The problem is that every new row I create is blank. I looped through the cells in the row and found that most of them were spaces ( ). I've looked everywhere for a different piece of code to use, but everything leads me to: create a new datatable, add a row to it, set the gridview datasource to the datatable. I've used this control a dozen times before and never got this before, it's very odd.

Class Level Variable

public partial class DMREntry : System.Web.UI.Page
    private DataTable _Parts
        get { return (DataTable)ViewState["Parts"]; }
        set { ViewState.Add("Parts", value); }

The add row code. Note that my textboxes and my gridview are within an update panel.

        if (_Parts == null)
            _Parts = new DataTable();
            _Parts.Columns.Add("Part No");
            _Parts.Columns.Add("Vendor Part");
            _Parts.Columns.Add("PO Number");
            _Parts.Columns.Add("Delivery Date");
            _Parts.Columns.Add("Total Cost");

        DataRow dr = _Parts.NewRow();

        dr["Part No"] = txtPartNo.Text;
        dr["Qty"] = txtQty.Text;
        dr["Description"] = txtDescription.Text;
        dr["Vendor"] = txtVendor.Text;
        dr["Vendor Part"] = txtVendorPart.Text;
        dr["Cost"] = txtCost.Text;
        dr["PO Number"] = txtPONumber.Text;
        dr["Delivery Date"] = txtDeliveryDate.Text;
        dr["Total Cost"] = txtTotalCost.Text;
        // At this point, DR has the correct values

        gvParts.DataSource = _Parts;


GridView markup:

`<asp:GridView ID="gvParts" runat="server" AutoGenerateColumns="False" PageSize="5" 
    Width="787px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
    BorderWidth="1px" CellPadding="3" 
            onselectedindexchanged="gvParts_SelectedIndexChanged1" AllowPaging="True" 
            <asp:TemplateField HeaderText="Part No"></asp:TemplateField>
            <asp:TemplateField HeaderText="Qty"></asp:TemplateField>
            <asp:TemplateField HeaderText="Description"></asp:TemplateField>
            <asp:TemplateField HeaderText="Vendor"></asp:TemplateField>
            <asp:TemplateField HeaderText="Vendor Part"></asp:TemplateField>
            <asp:TemplateField HeaderText="Cost"></asp:TemplateField>
            <asp:TemplateField HeaderText="PO Number"></asp:TemplateField>
            <asp:TemplateField HeaderText="Delivery Date"></asp:TemplateField>
            <asp:TemplateField HeaderText="Total Cost"></asp:TemplateField>
            <asp:CommandField ButtonType="Button" ShowEditButton="True" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#007DBB" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#00547E" />

Leaves me with:

empty gridview

My grid properties are the default, except:

  • AutoGenerateColumns = false
  • AllowPaging = true
  • AllowSorting = true
  • PageSize = 5

If I turn AutoGenerateColumns on, the data from the table does go into the auto-generated columns. But not for when I create the columns manually. I am using the template field column type for the manually added columns. They're just text fields, I couldn't find a better option.

Any help appreciated...

Related posts

Recent Viewed