Solved

how to fix my gridview from doubling the columns

Posted on 2011-03-14
3
377 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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