Murray Brown
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:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="B">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions2" runat="server">
<asp:ListItem></asp:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions3" runat="server">
<asp:ListItem></asp:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="D">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions4" runat="server">
<asp:ListItem></asp:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</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>
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:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="B">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions2" runat="server">
<asp:ListItem></asp:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions3" runat="server">
<asp:ListItem></asp:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="D">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions4" runat="server">
<asp:ListItem></asp:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</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>
can you post your .cs code for binding gridview.
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
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
ASKER
I built some code that loops through the cells, picking up their values and once it had finished
the missing DropDownList appeared
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.
Post complete HTML code of GridView.
ASKER
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:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="B">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions2" runat="server">
<asp:ListItem></asp:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions3" runat="server">
<asp:ListItem></asp:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="D">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions4" runat="server">
<asp:ListItem></asp:ListIt em>
<asp:ListItem>1st</asp:Lis tItem>
<asp:ListItem>2nd</asp:Lis tItem>
<asp:ListItem>3rd</asp:Lis tItem>
<asp:ListItem>4th</asp:Lis tItem>
</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>
<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:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="B">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions2" runat="server">
<asp:ListItem></asp:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="C">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions3" runat="server">
<asp:ListItem></asp:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="D">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions4" runat="server">
<asp:ListItem></asp:ListIt
<asp:ListItem>1st</asp:Lis
<asp:ListItem>2nd</asp:Lis
<asp:ListItem>3rd</asp:Lis
<asp:ListItem>4th</asp:Lis
</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?
ASKER
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(By Val oSQL As String)
Try
Dim cs As String = ConfigurationManager.Conne ctionStrin gs("Tests_ conn").Con nectionStr ing
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
All my GridView loads are a set of Questions, where the user selects answers from the template dropdowns
Sub Load_Questions_GridView(By
Try
Dim cs As String = ConfigurationManager.Conne
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help