473,598 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with a Gridview

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="Defau lt.aspx.vb"
Inherits="_Defa ult" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateCol umns="False" ShowHeader="fal se"
OnRowCommand="G ridView1_RowCom mand" Width="100%"
EnableViewState ="true">
<Columns>
<asp:ButtonFiel d ButtonType="But ton"
CommandName="bt nCollapse" Text="-">
<ControlStyle Font-Size="XX-Small" Height="18px"
Width="18px" />
<ItemStyle VerticalAlign=" Top" />
</asp:ButtonField >
<asp:BoundFie ld DataField="Logs " HtmlEncode="Fal se">
<ItemStyle VerticalAlign=" Top" Wrap="True" />
</asp:BoundField>
</Columns>
<EmptyDataRowSt yle Height="40px" />
<HeaderStyle BackColor="Gain sboro" />
<AlternatingRow Style BackColor="Tran sparent" />
<RowStyle BackColor="Gain sboro" />
</asp:GridView>
</div>
</form>
</body>
</html>
Partial Class _Default
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) 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.Da taSource = dt
Me.GridView1.Da taBind()

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).Cell s(0).Controls(0 ).Visible = False
Next
Catch ex As System.Exceptio n
j = 0
End Try

End Sub

Sub GridView1_RowCo mmand(ByVal sender As Object, ByVal e As
GridViewCommand EventArgs)

Dim i As Integer = Convert.ToInt32 (e.CommandArgum ent)
Dim iSelectedRow As GridViewRow = Me.GridView1.Ro ws(i)
Dim iDetailsRow As GridViewRow = Me.GridView1.Ro ws(i + 1)
Dim btn As Button = iSelectedRow.Ce lls(0).Controls (0)

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

End Sub

End Class
<?xml version="1.0"?>
<configuratio n>
<appSettings/>
<connectionStri ngs/>
<system.web>
<compilation debug="true" strict="false" explicit="true"/>
<pages>
<namespaces>
<clear/>
<add namespace="Syst em"/>
<add namespace="Syst em.Collections"/>
<add namespace="Syst em.Collections. Specialized"/>
<add namespace="Syst em.Configuratio n"/>
<add namespace="Syst em.Text"/>
<add namespace="Syst em.Text.Regular Expressions"/>
<add namespace="Syst em.Web"/>
<add namespace="Syst em.Web.Caching"/>
<add namespace="Syst em.Web.SessionS tate"/>
<add namespace="Syst em.Web.Security "/>
<add namespace="Syst em.Web.Profile"/>
<add namespace="Syst em.Web.UI"/>
<add namespace="Syst em.Web.UI.WebCo ntrols"/>
<add namespace="Syst em.Web.UI.WebCo ntrols.WebParts "/>
<add namespace="Syst em.Web.UI.HtmlC ontrols"/>
<add namespace="Syst em.Data"/>
</namespaces>
</pages>
<authenticati on mode="Windows"/>
</system.web>
</configuration>

Mar 1 '07 #1
3 2258
Can someone test this code and give me some pointers?
Thank you very much!
"sharris123 45" wrote:
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="Defau lt.aspx.vb"
Inherits="_Defa ult" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateCol umns="False" ShowHeader="fal se"
OnRowCommand="G ridView1_RowCom mand" Width="100%"
EnableViewState ="true">
<Columns>
<asp:ButtonFiel d ButtonType="But ton"
CommandName="bt nCollapse" Text="-">
<ControlStyle Font-Size="XX-Small" Height="18px"
Width="18px" />
<ItemStyle VerticalAlign=" Top" />
</asp:ButtonField >
<asp:BoundFie ld DataField="Logs " HtmlEncode="Fal se">
<ItemStyle VerticalAlign=" Top" Wrap="True" />
</asp:BoundField>
</Columns>
<EmptyDataRowSt yle Height="40px" />
<HeaderStyle BackColor="Gain sboro" />
<AlternatingRow Style BackColor="Tran sparent" />
<RowStyle BackColor="Gain sboro" />
</asp:GridView>
</div>
</form>
</body>
</html>
Partial Class _Default
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) 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.Da taSource = dt
Me.GridView1.Da taBind()

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).Cell s(0).Controls(0 ).Visible = False
Next
Catch ex As System.Exceptio n
j = 0
End Try

End Sub

Sub GridView1_RowCo mmand(ByVal sender As Object, ByVal e As
GridViewCommand EventArgs)

Dim i As Integer = Convert.ToInt32 (e.CommandArgum ent)
Dim iSelectedRow As GridViewRow = Me.GridView1.Ro ws(i)
Dim iDetailsRow As GridViewRow = Me.GridView1.Ro ws(i + 1)
Dim btn As Button = iSelectedRow.Ce lls(0).Controls (0)

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

End Sub

End Class
<?xml version="1.0"?>
<configuratio n>
<appSettings/>
<connectionStri ngs/>
<system.web>
<compilation debug="true" strict="false" explicit="true"/>
<pages>
<namespaces>
<clear/>
<add namespace="Syst em"/>
<add namespace="Syst em.Collections"/>
<add namespace="Syst em.Collections. Specialized"/>
<add namespace="Syst em.Configuratio n"/>
<add namespace="Syst em.Text"/>
<add namespace="Syst em.Text.Regular Expressions"/>
<add namespace="Syst em.Web"/>
<add namespace="Syst em.Web.Caching"/>
<add namespace="Syst em.Web.SessionS tate"/>
<add namespace="Syst em.Web.Security "/>
<add namespace="Syst em.Web.Profile"/>
<add namespace="Syst em.Web.UI"/>
<add namespace="Syst em.Web.UI.WebCo ntrols"/>
<add namespace="Syst em.Web.UI.WebCo ntrols.WebParts "/>
<add namespace="Syst em.Web.UI.HtmlC ontrols"/>
<add namespace="Syst em.Data"/>
</namespaces>
</pages>
<authenticati on mode="Windows"/>
</system.web>
</configuration>
Mar 9 '07 #2
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
sharris12345 schrieb:
>Partial Class _Default
Inherits System.Web.UI.P age

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventAr gs) Handles Me.Load
Call FillGridView()
(.. the sub FillGridView does a DataBind)
Mar 13 '07 #3
Thank you very much.
That was it.

Now I'm really embarrassed for my post!
"Roland Dick" wrote:
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
sharris12345 schrieb:
Partial Class _Default
Inherits System.Web.UI.P age

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

(.. the sub FillGridView does a DataBind)
Mar 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
729
by: Martin B | last post by:
Hallo to everyone! Problem: -------- GridView Exception: reentrant call to the SetCurrentCellAddressCore function System: ------- WinXP Professional, english, .NET Framework 2.0 Beta Language C#
3
13735
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
2
2129
by: guanfenglin | last post by:
Hello, I have a grid view which display and updates the data, however it doesn't work well, I always either get invalid name/number or not all varaibles bound, I am very frustrated at the moment. Thank you very much!! Here is the gridview code:
3
4597
by: Jeff | last post by:
Hey asp.net 2.0 In the source I posted below, there is a GridView (look at the bottom of the script): <asp:GridView ID="gvwOnline" runat="server"> </asp:GridView> I'm trying to assign a datasource to this GridView in runtime. But I cannot
13
10769
by: Tomasz Jastrzebski | last post by:
Helo All, The problem: GridView control does not render at all (header/footer) when the data source is empty. I have seen a similar question posted already, but I just can not believe there is no simple solution. I do not believe ASP.Net team took this, sort of, innovative approach and left no way out. Could anybody advise?
2
13169
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few fields to be included in the table, which is to be bound to the gridview. The web form looks like so (Don't worry about the stupidity of this web form for now.):
2
2813
by: GISmatters | last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files comprising each report. I want the user to be able to select an arbitrary collection of report files and have them emailed by clicking an "Email selected files" button. Everything displays properly, including the checkboxes for each child row (each...
2
1059
by: RobertTheProgrammer | last post by:
Hi all, I'm really stuck here. I've got a GridView on my page and I want to use the update features to-- well, update. Here is my block of code in the SqlDataSource section: <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringTraining %>" ProviderName="<%$ ConnectionStrings:ConnectionStringTraining.ProviderName %>" SelectCommand="SELECT...
6
5746
by: RobertTheProgrammer | last post by:
Hi folks, Here's a weird problem... I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the OnSelectedIndexChanged event set on it. This triggers just fine, but within the codebehind of the OnSelectedIndexChanged event, I need to scan through all the entries in the nested GridView (to see if the user changed a value to an already existing value in the...
4
2982
by: Ananthu | last post by:
I have created a templatefield at runtime for checkboxcolumn in gridview using Itemplate Interface and InstanceIn methods. I have given the id for checkbox also. I get the checkbox column in gridview successfully. Now i want to check all the checkbox in gridview. But when i find checkbox controls in gridview with respect to the id i have given, it returns me nothing.
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6711
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2410
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.