Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net GridView Template column behaving strangely

Hi

I have the following GridView with four DropDownList template columns.
After data binding the second one doesn't show properly.
I can't find any of my code that would affect it. The image shows how it looks

<asp:GridView ID="GridView2" runat="server"
    font-names="Century Gothic" forecolor="White" style="margin-top: 0px">
                <Columns>
                   
                     <asp:TemplateField HeaderText="A">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>

             


                     <asp:TemplateField HeaderText="B">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions2" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>

                     <asp:TemplateField HeaderText="C">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions3" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>

                     <asp:TemplateField HeaderText="D">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions4" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>
                   

                    <asp:TemplateField HeaderText="Question">
                        <ItemTemplate>
                         </ItemTemplate>
                    </asp:TemplateField>




                    <asp:TemplateField HeaderText="Answer">
                   
                        <ControlStyle BackColor="#CCCCCC" />
                   
                    </asp:TemplateField>




                </Columns>
            </asp:GridView>

User generated image
Avatar of jitendra patil
jitendra patil
Flag of India image

can you post your .cs code for binding gridview.
Avatar of Murray Brown

ASKER

Sub Load_GridView_with_Survey(ByVal oSQL As String)

        Try

            ' Run the query and bind the resulting DataSet
            ' to the GridView control.
            Dim ds2 As DataSet = GetData(oSQL)
            If (ds2.Tables.Count > 0) Then

                Me.GridView2.DataSource = ds2
                Me.GridView2.DataBind()

            Else

                'Me.Label_Error1.Text = "Unable to connect to the database. " & Err.Description

            End If


        Catch ex As Exception
            Me.Label1.Text = ex.Message
        End Try
    End Sub
I built some code that loops through the cells, picking up their values and once it had finished
the missing DropDownList appeared
What exactly you are getting in DataSet? Are there any bound fields in your GridView code?

Post complete HTML code of GridView.
I bind data from a SQL database to the GridView
<asp:GridView ID="GridView2" runat="server"
    font-names="Century Gothic" forecolor="White" style="margin-top: 0px">
                <Columns>
                   
                     <asp:TemplateField HeaderText="A">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>

             


                     <asp:TemplateField HeaderText="B">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions2" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>

                     <asp:TemplateField HeaderText="C">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions3" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>

                     <asp:TemplateField HeaderText="D">
                     <ItemTemplate>
                          <asp:DropDownList ID="ddlOptions4" runat="server">
                                <asp:ListItem></asp:ListItem>
                                <asp:ListItem>1st</asp:ListItem>
                                <asp:ListItem>2nd</asp:ListItem>
                                <asp:ListItem>3rd</asp:ListItem>
                                <asp:ListItem>4th</asp:ListItem>
                         </asp:DropDownList>
                     </ItemTemplate>
                     </asp:TemplateField>
                   

                    <asp:TemplateField HeaderText="Question">
                        <ItemTemplate>
                         </ItemTemplate>
                    </asp:TemplateField>




                    <asp:TemplateField HeaderText="Answer">
                   
                        <ControlStyle BackColor="#CCCCCC" />
                   
                    </asp:TemplateField>




                </Columns>
            </asp:GridView>
Here I am not seeing any bound fields. Why you are binding with a DataSet? What you are getting in DataSet and what you are trying to achieve?
I changed the binding code to the following and the same thing happened.
All my GridView loads are a set of Questions, where the user selects answers from the template dropdowns


    Sub Load_Questions_GridView(ByVal oSQL As String)
        Try

            Dim cs As String = ConfigurationManager.ConnectionStrings("Tests_conn").ConnectionString
            Dim connection As New SqlConnection(cs)
            Dim dataadapter As New SqlDataAdapter(oSQL, connection)
            Dim ds As New DataSet()
            connection.Open()
            dataadapter.Fill(ds, "My_table")
            connection.Close()
            GridView2.DataSource = ds
            GridView2.DataMember = "My_table"
            GridView2.DataBind()

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help