Problem with a Gridview

G

Guest

Hi,

I have a GridView that has one column that I fill with an alternating "Title
Row" then "Details Row". The Title row has a button that I use to hide it's
corresponding details row that is underneath it for users to collapse detail
rows leaving the title row visible. The button can be clicked again to show
the details.

This used to work great using a DataGrid but when I upgraded it to a
GridView I experience the following problem:

When clicking the button it hides the details row as expected but when
clicking other buttons, the previously hidden details row show. They do not
retain their hidden attributes.

Can someone help me with this? THANKS!
Here's the code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" ShowHeader="false"
OnRowCommand="GridView1_RowCommand" Width="100%"
EnableViewState="true">
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="btnCollapse" Text="-">
<ControlStyle Font-Size="XX-Small" Height="18px"
Width="18px" />
<ItemStyle VerticalAlign="Top" />
</asp:ButtonField>
<asp:BoundField DataField="Logs" HtmlEncode="False">
<ItemStyle VerticalAlign="Top" Wrap="True" />
</asp:BoundField>
</Columns>
<EmptyDataRowStyle Height="40px" />
<HeaderStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="Transparent" />
<RowStyle BackColor="Gainsboro" />
</asp:GridView>
</div>
</form>
</body>
</html>


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Call FillGridView()
End Sub

Private Sub FillGridView()

Dim j As Short
Dim dt As New DataTable

dt.Columns.Add("Logs", GetType(System.String))

For j = 1 To 5
Dim dr1 As DataRow = dt.NewRow
dr1(0) = "Title " & j.ToString
dt.Rows.Add(dr1)

Dim dr2 As DataRow = dt.NewRow
dr2(0) = "Details here ..."
dt.Rows.Add(dr2)
Next

Me.GridView1.DataSource = dt
Me.GridView1.DataBind()

Call CleanUpButtons(Me.GridView1)

End Sub

Private Sub CleanUpButtons(ByVal gv As GridView)

Dim j As Integer

Try
For j = 1 To gv.Rows.Count Step 2
gv.Rows(j).Cells(0).Controls(0).Visible = False
Next
Catch ex As System.Exception
j = 0
End Try

End Sub

Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
GridViewCommandEventArgs)

Dim i As Integer = Convert.ToInt32(e.CommandArgument)
Dim iSelectedRow As GridViewRow = Me.GridView1.Rows(i)
Dim iDetailsRow As GridViewRow = Me.GridView1.Rows(i + 1)
Dim btn As Button = iSelectedRow.Cells(0).Controls(0)

If btn.Text = "-" Then
iDetailsRow.Visible = False
btn.Text = "+"
Else
iDetailsRow.Visible = True
btn.Text = "-"
End If

End Sub

End Class


<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="true" strict="false" explicit="true"/>
<pages>
<namespaces>
<clear/>
<add namespace="System"/>
<add namespace="System.Collections"/>
<add namespace="System.Collections.Specialized"/>
<add namespace="System.Configuration"/>
<add namespace="System.Text"/>
<add namespace="System.Text.RegularExpressions"/>
<add namespace="System.Web"/>
<add namespace="System.Web.Caching"/>
<add namespace="System.Web.SessionState"/>
<add namespace="System.Web.Security"/>
<add namespace="System.Web.Profile"/>
<add namespace="System.Web.UI"/>
<add namespace="System.Web.UI.WebControls"/>
<add namespace="System.Web.UI.WebControls.WebParts"/>
<add namespace="System.Web.UI.HtmlControls"/>
<add namespace="System.Data"/>
</namespaces>
</pages>
<authentication mode="Windows"/>
</system.web>
</configuration>
 
R

Roland Dick

Hi sharris,

I haven't tried your code, but from looking at it, it makes sense -
you're binding the grdiview in every Page_Load which causes it to
re-create each row and therefore "forget" about the Visible information.

Try to only bind the GridView in the Page_Load if it is not a postback
request.

Hope this helps,

Cheers,

Roland


(.. the sub FillGridView does a DataBind)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,063
Latest member
StormyShuf

Latest Threads

Top