Solved

how to fix my gridview from doubling the columns

Posted on 2011-03-14
3
376 Views
Last Modified: 2012-05-11
I have a gridview with template items and am binding the gridview with a data table. What is happening is that the columns with the textboxes, etc are populating, but then extra columns from the data table with the same data is being added to the end of the gridview. code is attached. I tried to clear the gridview, but it got rid of the template item fields.
aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            //forces postback when the treeview iten's check box is clicked.
            tvContracts.Attributes.Add("onclick", "postBackByObject()");

            ManagedUser = (Administrator)HttpContext.Current.Session["UserObject"];

            //Populating all drop downs for the contract modal window.
            if(!IsPostBack)
            {
CallOptions.fillEmptyCallOptionGV(CallOptionGV);
            }
        }


calloption.cs

public void fillEmptyCallOptionGV(GridView GV)
        {
            dt = new DataTable();

            dt.Columns.Add("NAME");
            dt.Columns.Add("CALL_OPTION_NAME");
            dt.Columns.Add("COUNTERPARTY_NOTIFICATIONS");
            dt.Columns.Add("EMO_NOTIFICATIONS");
            dt.Columns.Add("COUNTERPARTY_EMAILS");
            dt.Columns.Add("EMO_EMAILS");
            dt.Columns.Add("TRADES_DEAL_NUMBER");
            dt.Columns.Add("DEFAULT_TO_AVAILABLE");
            dt.Columns.Add("POR_AVAILABILITY");
            dt.Columns.Add("MW");
            dt.Columns.Add("ALLOW_ADDITIONAL_MW");
            dt.Columns.Add("SELLER_AVAILABILITY_BY");
            dt.Columns.Add("EMO_STRIKE_NOTICE_BY");
            dt.Columns.Add("MIN_RUN_TIME");
            dt.Columns.Add("MIN_DOWN_TIME");
            dt.Columns.Add("STARTS_PER_DAY");
            dt.Columns.Add("EMO_INTRADAY_CHANGES_BY");
            dt.Columns.Add("MIN_CONTRACT_STARTS");
            dt.Columns.Add("MAX_CONTRACT_STARTS");
            dt.Columns.Add("ADDITIONAL_START_CHARGE");
            dt.Columns.Add("COMMENTS");
            dt.Columns.Add("ID");

            dt.Rows.Add(new object[] { "A", "", "0", "0", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", "1" });
            dt.Rows.Add(new object[] { "B", "", "0", "0", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", "2" });
            dt.Rows.Add(new object[] { "C", "", "0", "0", "", "", "", "1", "", "", "", "", "", "", "", "", "", "", "", "", "", "3" });

            GV.DataSource = dt;
            GV.DataBind();

        }

Open in new window

<asp:GridView ID="CallOptionGV" runat="server" BackColor="White" Width="1200" OnRowDataBinding="CallOptionGV_RowDataBound" DataKeyNames="ID" 
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="Small"
        ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" Visible="false" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
                <asp:TemplateField HeaderText="Product Name" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtProductName" runat="server" Width="20" MaxLength="1" Text='<%# Bind("NAME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Call Option Name" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtCallOptionName" runat="server" Width="75" Text='<%# Bind("CALL_OPTION_NAME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Counterparty Notifications" InsertVisible="true">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RdCounterNotification" runat="server">
                            <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
                            <asp:ListItem Text="No" Value="1"></asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="EMO Notifications" InsertVisible="true">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RdEMONotification" runat="server">
                            <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
                            <asp:ListItem Text="No" Value="1"></asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Counterparty Email Groups" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtCounterEmail" runat="server" Width="100" Text='<%# Bind("COUNTERPARTY_EMAILS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMO Email Groups" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtEMOEmail" runat="server" Width="100" Text='<%# Bind("EMO_EMAILS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Trades Deal Number" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtTradesNo" runat="server" Width="70" Text='<%# Bind("TRADES_DEAL_NUMBER") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Availability Default to Option Volume" InsertVisible="true">
                    <ItemTemplate>
                        <asp:RadioButtonList ID="RdDftOptionVol" runat="server">
                            <asp:ListItem>Yes</asp:ListItem>
                            <asp:ListItem>No</asp:ListItem>
                        </asp:RadioButtonList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MW" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMW" runat="server" Width="50" Text='<%# Bind("MW") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Allow Additional MW" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAddMW" runat="server" Width="50" Text='<%# Bind("ALLOW_ADDITIONAL_MW") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Seller to Provide Availability by" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtSellerBy" runat="server" Width="50" Text='<%# Bind("SELLER_AVAILABILITY_BY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMO to Provide Schedule/Strike Notice by" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtEMOBy" runat="server" Width="50" Text='<%# Bind("EMO_STRIKE_NOTICE_BY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Minimum Run Time" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMinRunTime" runat="server" Width="50" Text='<%# Bind("MIN_RUN_TIME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Minimum Down Time" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMinDwnTime" runat="server" Width="50" Text='<%# Bind("MIN_DOWN_TIME") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Starts Per Day" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtStartsPerDay" runat="server" Width="30" Text='<%# Bind("STARTS_PER_DAY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMO to Provide Intraday Changes by" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtEMOIntraChg" runat="server" Width="50" Text='<%# Bind("EMO_INTRADAY_CHANGES_BY") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Minimum Starts During Contract Term" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMinStarts" runat="server" Width="30" Text='<%# Bind("MIN_CONTRACT_STARTS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Maximum Starts During Contract Term" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtMaxStarts" runat="server" Width="30" Text='<%# Bind("MAX_CONTRACT_STARTS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Additional Start Charge for Dispatch > 24 Hours" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtStartCharge" runat="server" Width="80" Text='<%# Bind("ADDITIONAL_START_CHARGE") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Comments" InsertVisible="true">
                    <ItemTemplate>
                        <asp:TextBox ID="txtCallOptionComments" runat="server" Width="100" Text='<%# Bind("COMMENTS") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>    
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FBFBF2" />
            <SortedAscendingHeaderStyle BackColor="#848384" />
            <SortedDescendingCellStyle BackColor="#EAEAD3" />
            <SortedDescendingHeaderStyle BackColor="#575357" />
        </asp:GridView>

Open in new window

0
Comment
Question by:bschave2
  • 2
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35132822
You need to add AutoGenerateColumns="false" to your gridview definition in order to stop it auto-creating them.
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 35132854
Set the AutoGenerateColumns attribute to false :

<asp:GridView ID="CallOptionGV" runat="server" BackColor="White" Width="1200" OnRowDataBinding="CallOptionGV_RowDataBound" DataKeyNames="ID"
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Size="Small"
        ForeColor="Black" GridLines="Vertical" AutoGenerateCoumns="False">
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35187033
Pretty sure that's exactly the same thing I said!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now