Mangesh Kaslikar
Mangesh Kaslikar

Reputation: 591

Why are there unwanted duplicated and extra columns in Asp:GridView?

I am trying to show some Details on my Web-Page using the ASP:GridView Control. Accordingly, I have added the columns that I need to show. But, every column is shown twice (Pic) in the GridView . enter image description here

By back end Code is as follows:

objVendor = client.GetAllVenorsForPCMS();

        if (objVendor.Count > 0)
        {

            gvVendorsDetails.DataSource = objVendor;
            gvVendorsDetails.DataBind();

        }

        else
        {
            gvVendorsDetails.DataSource = null;
            gvVendorsDetails.DataBind();
        }

and aspx Code as follows :

                <div align="center" style="border: 1px solid;">
                    <asp:GridView ID="gvVendorsDetails" runat="server" CssClass="mGrid">
                        <Columns>
                            <asp:BoundField HeaderText="Vendor ID" DataField="VendorID" Visible="false" />
                            <asp:BoundField HeaderText="Vendor Name" DataField="VendorName" Visible="true" />
                            <asp:BoundField HeaderText="Vendor Description" DataField="VendorDescription" Visible="true" />
                            <asp:BoundField HeaderText="Address" DataField="Address" Visible="true" />
                            <asp:BoundField HeaderText="City" DataField="City" Visible="true" />
                            <asp:BoundField HeaderText="State" DataField="State" Visible="true" />
                            <asp:BoundField HeaderText="Country" DataField="Country" Visible="true" />
                            <asp:BoundField HeaderText="Contact Person" DataField="ContactPerson" Visible="true" />
                            <asp:BoundField HeaderText="Contact No" DataField="ContactNo" Visible="true" />
                            <asp:BoundField HeaderText="ZIP Code" DataField="ZIPCode" Visible="true" />
                        </Columns>
                    </asp:GridView>
                </div>

I have added the columns only once, but how come in the result columns are shown Twice !! ?

Upvotes: 5

Views: 3169

Answers (1)

StuartLC
StuartLC

Reputation: 107317

A common reason for this is because you also have the AutoGenerateColumns property set to true (which is the default). By setting the property to false will limit the columns generated to just those you have specified explicitly.

i.e. fix this like so:

<asp:GridView ID="gvVendorsDetails" runat="server" 
              CssClass="mGrid" AutoGenerateColumns="False">
     <Columns> ...

Upvotes: 12

Related Questions