Hi guys,

I have created a gridview which is supposed to display 3 columns:

| view | Name | Position |

The gridview becomes visible based a a selection of a dropdownlist. When I load the gridview from the dropdownlist however, the grid view appears as such:

| view | Name | Position | Name | Position |

Why are the columns repeating and how do I resolve this issue?

here is the markup for the dropdownlist and its corresponding gridview:

    <div>
        <table width="80%" align="center">
            <tr>
                <td class="auto-style1">
                    <asp:Label ID="Lbl_SelectTeam" runat="server" Text="Select a Team" />
                </td>
                <td>
                    <asp:DropDownList ID="Ddl_Teams" runat="server" style="margin-left: 0px" Width="150px"/>
                </td>
            </tr>
            <tr>
                <td class="auto-style1"></td>
                <td>
                    <asp:Button ID="Btn_SubmitTeam" runat="server" Text="Select" OnClick="TeamPage_Click" />
                </td>
            </tr>
        </table>
    </div>
    <div class="GridView">
        <asp:GridView ID="Gv_PlayersOfTeamView" runat="server" Visible="false" 
            GridLines="Horizontal" Width="500" >
            <Columns>
                <asp:TemplateField ShowHeader="false">
                    <ItemTemplate>
                        <asp:LinkButton ID="Lb_PlayerProfile" runat="server" Text="View" 
                            ForeColor="white" Font-Bold="true" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Center" 
                    ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Label ID="Lbl_PlayerName" runat="server" Text='<%# Eval("Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Position" HeaderStyle-HorizontalAlign="Center" 
                    ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:Label ID="Lbl_PlayerPosition" runat="server" Text='<%# Eval("Position") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

here is the codebehind function which is called when the "Select" button is clicked for the dropdownlist:

        protected void TeamPage_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                String TeamID = Ddl_Teams.SelectedValue;
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "SELECT DISTINCT Name, Position FROM FB_Players WHERE TeamID=@teamid ORDER BY Name";
                cmd.Parameters.AddWithValue("@teamid", TeamID);
                con.Open();
                Gv_PlayersOfTeamView.DataSource = cmd.ExecuteReader();
                Gv_PlayersOfTeamView.DataBind();
                con.Close();
            }

            Gv_PlayersOfTeamView.Visible = true;
        }

Thank you for your time and help

Recommended Answers

All 3 Replies

Weird. In the design view, click on the grid view, edit columns. Do you see three or five?

Set the autogeneratecolumns property to false

nakor77,

Thanks that worked just liked I wanted!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.