Solved

how to fix my gridview from doubling the columns

Posted on 2011-03-14
3
373 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
Comment Utility
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
Comment Utility
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
Comment Utility
Pretty sure that's exactly the same thing I said!!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Where to begin studying? 5 63
Pass through dll 2 33
ConsoleSql 1 12
Get String split 5 31
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

10 Experts available now in Live!

Get 1:1 Help Now