Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. GridView row dissappearing on SelectedIndexChanged

GridView row dissappearing on SelectedIndexChanged

Scheduled Pinned Locked Moved ASP.NET
helpwpfwcfperformancequestion
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    binarymax
    wrote on last edited by
    #1

    Hi, I have a crazy problem where when I select any row(call it rowA) in a gridview, and then when I select any different row (call it rowB), rowA dissappears. If I then select another row (rowC), rowB dissappears, and so on. These rows are not in any specific order and it happens with any case. I really want all the rows to stay there! As a temporary hack I am re-binding on every select, and I am pretty sure this causes a performance issue so I really don't want to keep it this way. Is there any way for me to select different rows while by default keeping them all visible? Many thanks in advance! Max.

    C 1 Reply Last reply
    0
    • B binarymax

      Hi, I have a crazy problem where when I select any row(call it rowA) in a gridview, and then when I select any different row (call it rowB), rowA dissappears. If I then select another row (rowC), rowB dissappears, and so on. These rows are not in any specific order and it happens with any case. I really want all the rows to stay there! As a temporary hack I am re-binding on every select, and I am pretty sure this causes a performance issue so I really don't want to keep it this way. Is there any way for me to select different rows while by default keeping them all visible? Many thanks in advance! Max.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      What do you mean by 'select' ? What is your code that runs when you 'select' a row ?

      Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

      B 1 Reply Last reply
      0
      • C Christian Graus

        What do you mean by 'select' ? What is your code that runs when you 'select' a row ?

        Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

        B Offline
        B Offline
        binarymax
        wrote on last edited by
        #3

        Right, it might help if I show the code!

        <asp:GridView ID="gvIncidents" runat="server" AllowSorting="true" AutoGenerateColumns="false" onrowcommand="gvIncidents\_RowCommand">
        <HeaderStyle ForeColor="#606060"></HeaderStyle>
        <Columns>
            <asp:BoundField DataField="FormID" HeaderText="" />
            <asp:BoundField DataField="FormType" HeaderText="Type" />
            <asp:BoundField DataField="FormDateTime" HeaderText="Date/Time" />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            <asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
            <asp:ButtonField CommandName="Import" Text="Import" ButtonType="Button" />
            <asp:ButtonField CommandName="Deny" Text="Delete" ButtonType="Button" />
        </Columns>
        </asp:GridView>
        

        In my page load:

                ...
                Me.gvIncidents.DataSource = dtIncidents
                Me.gvIncidents.DataBind()
                Me.gvIncidents.Visible = True
                Me.gvIncidents.Columns(0).Visible = False
                ...
        

        Handlers:

        Protected Sub gvIncidents\_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvIncidents.RowDataBound
        
            Try
        
                With e.Row
                    If .RowType = DataControlRowType.DataRow Then
        
                        .CssClass = "gridrow"
        
                        'Change the cursor on mouseover
                        .Cells(2).Attributes.Add("onMouseOver", "this.style.cursor='hand';")
                        .Cells(2).Attributes.Add("onclick", ClientScript.GetPostBackEventReference(gvIncidents, "Select$" + .RowIndex.ToString()))
        
                    Else 'Header
                        .CssClass = "gridtitle"
        
                    End If
                End With
        
            Catch ex As Exception
                Throw ex
            End Try
        
        End Sub
        
        Protected Sub gvIncidents\_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvIncidents.SelectedIndexChanged
            Dim oGrid As GridView
            Dim oRow As GridViewRow
        
            Try
                oGrid = CType(sender, GridView)
                oRow = oGrid.SelectedRow
        
                PopulateIncidents() 'temporary hack to stop the rows from dissappearing
        
                ShowIncidentDetails(CType(oRow.Cells(0).Text, Integer))
        
            Catch ex As Exception
        
            End Try
        End Sub
        
        C H 2 Replies Last reply
        0
        • B binarymax

          Right, it might help if I show the code!

          <asp:GridView ID="gvIncidents" runat="server" AllowSorting="true" AutoGenerateColumns="false" onrowcommand="gvIncidents\_RowCommand">
          <HeaderStyle ForeColor="#606060"></HeaderStyle>
          <Columns>
              <asp:BoundField DataField="FormID" HeaderText="" />
              <asp:BoundField DataField="FormType" HeaderText="Type" />
              <asp:BoundField DataField="FormDateTime" HeaderText="Date/Time" />
              <asp:BoundField DataField="Description" HeaderText="Description" />
              <asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
              <asp:ButtonField CommandName="Import" Text="Import" ButtonType="Button" />
              <asp:ButtonField CommandName="Deny" Text="Delete" ButtonType="Button" />
          </Columns>
          </asp:GridView>
          

          In my page load:

                  ...
                  Me.gvIncidents.DataSource = dtIncidents
                  Me.gvIncidents.DataBind()
                  Me.gvIncidents.Visible = True
                  Me.gvIncidents.Columns(0).Visible = False
                  ...
          

          Handlers:

          Protected Sub gvIncidents\_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvIncidents.RowDataBound
          
              Try
          
                  With e.Row
                      If .RowType = DataControlRowType.DataRow Then
          
                          .CssClass = "gridrow"
          
                          'Change the cursor on mouseover
                          .Cells(2).Attributes.Add("onMouseOver", "this.style.cursor='hand';")
                          .Cells(2).Attributes.Add("onclick", ClientScript.GetPostBackEventReference(gvIncidents, "Select$" + .RowIndex.ToString()))
          
                      Else 'Header
                          .CssClass = "gridtitle"
          
                      End If
                  End With
          
              Catch ex As Exception
                  Throw ex
              End Try
          
          End Sub
          
          Protected Sub gvIncidents\_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvIncidents.SelectedIndexChanged
              Dim oGrid As GridView
              Dim oRow As GridViewRow
          
              Try
                  oGrid = CType(sender, GridView)
                  oRow = oGrid.SelectedRow
          
                  PopulateIncidents() 'temporary hack to stop the rows from dissappearing
          
                  ShowIncidentDetails(CType(oRow.Cells(0).Text, Integer))
          
              Catch ex As Exception
          
              End Try
          End Sub
          
          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          binarymax wrote:

          Me.gvIncidents.Columns(0).Visible = False

          And is it always the top line that disappears ? What if you remove this line ? Do you databind on every postback ? I remember you saying you did, you're saying this solves the issue, but when you have the issue, this code is entirely in an !IsPostback block ? PopulateIncidents reloads the data ? What does ShowIncidentDetails do ? Have you tried commenting out sections of this code to work out which part is causing the issue ?

          Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

          B 1 Reply Last reply
          0
          • C Christian Graus

            binarymax wrote:

            Me.gvIncidents.Columns(0).Visible = False

            And is it always the top line that disappears ? What if you remove this line ? Do you databind on every postback ? I remember you saying you did, you're saying this solves the issue, but when you have the issue, this code is entirely in an !IsPostback block ? PopulateIncidents reloads the data ? What does ShowIncidentDetails do ? Have you tried commenting out sections of this code to work out which part is causing the issue ?

            Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

            B Offline
            B Offline
            binarymax
            wrote on last edited by
            #5

            Good questions, the row that is dissappearing is the last row you selected. So if I select the 5th row, and then the 7th row, row 5 dissappears. Right that page load is entirely in an !IsPostback block and it initially binds the data. PopulateIncidents is a function that re-binds the data. ShowIncidentDetails shows a panel with details about the row you selected, if I remove that the problem persists. Also I removed Me.gvIncidents.Columns(0).Visible = False from page load and still the same problem. The part that is causing the problem is having the SelectedIndexChanged event. I can strip out all the code from SelectedIndexChanged and the problem stays the same. If I remove the event entirely then the problem goes away, but I need the event in order to select the row, so I really can't remove it. Thanks.

            modified on Tuesday, May 5, 2009 5:57 AM

            1 Reply Last reply
            0
            • B binarymax

              Right, it might help if I show the code!

              <asp:GridView ID="gvIncidents" runat="server" AllowSorting="true" AutoGenerateColumns="false" onrowcommand="gvIncidents\_RowCommand">
              <HeaderStyle ForeColor="#606060"></HeaderStyle>
              <Columns>
                  <asp:BoundField DataField="FormID" HeaderText="" />
                  <asp:BoundField DataField="FormType" HeaderText="Type" />
                  <asp:BoundField DataField="FormDateTime" HeaderText="Date/Time" />
                  <asp:BoundField DataField="Description" HeaderText="Description" />
                  <asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
                  <asp:ButtonField CommandName="Import" Text="Import" ButtonType="Button" />
                  <asp:ButtonField CommandName="Deny" Text="Delete" ButtonType="Button" />
              </Columns>
              </asp:GridView>
              

              In my page load:

                      ...
                      Me.gvIncidents.DataSource = dtIncidents
                      Me.gvIncidents.DataBind()
                      Me.gvIncidents.Visible = True
                      Me.gvIncidents.Columns(0).Visible = False
                      ...
              

              Handlers:

              Protected Sub gvIncidents\_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvIncidents.RowDataBound
              
                  Try
              
                      With e.Row
                          If .RowType = DataControlRowType.DataRow Then
              
                              .CssClass = "gridrow"
              
                              'Change the cursor on mouseover
                              .Cells(2).Attributes.Add("onMouseOver", "this.style.cursor='hand';")
                              .Cells(2).Attributes.Add("onclick", ClientScript.GetPostBackEventReference(gvIncidents, "Select$" + .RowIndex.ToString()))
              
                          Else 'Header
                              .CssClass = "gridtitle"
              
                          End If
                      End With
              
                  Catch ex As Exception
                      Throw ex
                  End Try
              
              End Sub
              
              Protected Sub gvIncidents\_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvIncidents.SelectedIndexChanged
                  Dim oGrid As GridView
                  Dim oRow As GridViewRow
              
                  Try
                      oGrid = CType(sender, GridView)
                      oRow = oGrid.SelectedRow
              
                      PopulateIncidents() 'temporary hack to stop the rows from dissappearing
              
                      ShowIncidentDetails(CType(oRow.Cells(0).Text, Integer))
              
                  Catch ex As Exception
              
                  End Try
              End Sub
              
              H Offline
              H Offline
              Herman T Instance
              wrote on last edited by
              #6

              Why you have the line <asp:BoundField DataField="FormID" HeaderText="" /> in your ASP if your code behind removes the column. Or skip it Or you might want to add DataKeyNames to this line: <asp:GridView ID="gvIncidents" runat="server" AllowSorting="true" AutoGenerateColumns="false" onrowcommand="gvIncidents_RowCommand"> If you debug the SelectedIndexChanged event what is the value for oGrid.SelectedIndex? And is the oGrid.SelectedRow the row you selected?

              In Word you can only store 2 bytes. That is why I use Writer.

              B 1 Reply Last reply
              0
              • H Herman T Instance

                Why you have the line <asp:BoundField DataField="FormID" HeaderText="" /> in your ASP if your code behind removes the column. Or skip it Or you might want to add DataKeyNames to this line: <asp:GridView ID="gvIncidents" runat="server" AllowSorting="true" AutoGenerateColumns="false" onrowcommand="gvIncidents_RowCommand"> If you debug the SelectedIndexChanged event what is the value for oGrid.SelectedIndex? And is the oGrid.SelectedRow the row you selected?

                In Word you can only store 2 bytes. That is why I use Writer.

                B Offline
                B Offline
                binarymax
                wrote on last edited by
                #7

                Thanks for the reply, The FormID is bound because that is the PK that I don't want the user to see, but when the row is selected then I use FormID to lookup data so I can populate a panel with record details. Even when it is visible I still have the same problems. oGrid.SelectedIndex and SelectedRow are behaving as expected.

                H 1 Reply Last reply
                0
                • B binarymax

                  Thanks for the reply, The FormID is bound because that is the PK that I don't want the user to see, but when the row is selected then I use FormID to lookup data so I can populate a panel with record details. Even when it is visible I still have the same problems. oGrid.SelectedIndex and SelectedRow are behaving as expected.

                  H Offline
                  H Offline
                  Herman T Instance
                  wrote on last edited by
                  #8

                  that is why you can use the DataKeyNames to store your PK in. So not visiblle but accessible and no columns to hide. If you set the selectedIndex value, the property SelectedValue will contain the PK value. ShowIncidentDetails(CType(oRow.Cells(0).Text, Integer)) isn't cells[0] now formtype in stead of formID?

                  In Word you can only store 2 bytes. That is why I use Writer.

                  B 1 Reply Last reply
                  0
                  • H Herman T Instance

                    that is why you can use the DataKeyNames to store your PK in. So not visiblle but accessible and no columns to hide. If you set the selectedIndex value, the property SelectedValue will contain the PK value. ShowIncidentDetails(CType(oRow.Cells(0).Text, Integer)) isn't cells[0] now formtype in stead of formID?

                    In Word you can only store 2 bytes. That is why I use Writer.

                    B Offline
                    B Offline
                    binarymax
                    wrote on last edited by
                    #9

                    I can use DataKeyNames which is nice to know but unfortunately it doesnt solve the problem :( Also, even if I set visible=false on the first column the data is still bound as index 0 so cells[0] returns the formid as expected.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups