Click here to Skip to main content
14,603,943 members
Rate this:
Please Sign up or sign in to vote.
I have a Gridview that contains 3 columns. 1)Year 2)Year Start 3)Year End.
Column 2 and 3 is fine, but the first column contains a button, either it shows "Make this year active" or "Active year"(there can only be one active year). Now if I click on let's say row 3 (make this year active) and i go to page two of the grid view and then back to one, the third row is hilighted but the "Active button" is on first row and the row that should show active year shows "Make active year" and also page two first row shows "Active year" but there can only be one.

Protected Sub grdFinYear_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdFinYear.PageIndexChanging
        grdFinYear.PageIndex = e.NewPageIndex
        'grdFinYear_RowCreated()

        dsFinYear.SelectCommand = "Select * from FinancialYears Order By FIN_Year"
        dsFinYear.DataBind()
        grdFinYear.AutoGenerateColumns = False
        grdFinYear.Visible = True
        ' grdFinYear.DataBind()
    End Sub


Protected Sub grdFinYear_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdFinYear.RowCreated

        If grdFinYear.Rows.Count >= 1 Then
            Dim activeButton As Button = grdFinYear.Rows(0).FindControl("btnSelect")
            'Dim buttonDefault As Button = grdFinYear.Rows(0).FindControl("btnSelect")
            'buttonDefault.Text = "ACTIVE YEAR"
            'buttonDefault.Width = 175
            activeButton.CssClass = "ActionButtonsActiveYear"

            For i As Integer = 1 To grdFinYear.Rows.Count - 1
                Dim makeActiveButton As Button = grdFinYear.Rows(i).FindControl("btnSelect")
                'Dim button As Button = grdFinYear.Rows(i).FindControl("btnSelect")
                'button.Text = "MAKE THIS YEAR ACTIVE"
                'button.Width = 175
                makeActiveButton.CssClass = "ActionButtonsMakeThisYearActive"
            Next
        End If

    End Sub


Protected Sub grdFinYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        FIN_ID = grdFinYear.DataKeys(grdFinYear.SelectedRow.RowIndex).Values("FIN_ID")

        If grdFinYear.Rows.Count > 1 Then
            For i As Integer = 0 To grdFinYear.Rows.Count - 1
                If FIN_ID <> grdFinYear.DataKeys(grdFinYear.Rows(i).RowIndex).Values("FIN_ID") Then
                    Dim makeActiveButton As Button = grdFinYear.Rows(i).FindControl("btnSelect")
                    'Dim button As Button = grdFinYear.Rows(i).FindControl("btnSelect")
                    'button.Text = "MAKE THIS YEAR ACTIVE"
                    'button.Width = 175
                    makeActiveButton.CssClass = "ActionButtonsMakeThisYearActive"
                Else
                    Dim activeButton As Button = grdFinYear.Rows(i).FindControl("btnSelect")
                    'Dim button As Button = grdFinYear.Rows(i).FindControl("btnSelect")
                    'button.Text = "ACTIVE YEAR"
                    'button.Width = 175
                    activeButton.CssClass = "ActionButtonsActiveYear"
                End If
            Next
        End If

        grdPolicies.Visible = False
        trFilterPol.Visible = False
        trShowPol.Visible = False

        tblPolicyDetails.Visible = False

        Dim daFinYear As New SqlDataAdapter("Select * from FinancialYears where FIN_ID = @FIN_ID", MyConnManagement)
        Dim dsFinYear As New DataSet

        daFinYear.SelectCommand.Parameters.AddWithValue("FIN_ID", FIN_ID)
        daFinYear.Fill(dsFinYear, "FinYear")

        lblActiveFinYear.Text = dsFinYear.Tables("FinYear").Rows(0).Item("FIN_Name")

        '   To load existing policies
        '   -------------------------
        Call subLoadExistingPolisies()
    End Sub
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Your problem is with paging in Grid View, whenever for loop executed, its condition run only as many times as "Grid View Row Count" and your "Grid View Row Count" is max as Page Size like if you declare Page Size only 10 then your for loop will be executed only 10 times no matter your Grid View contains more than 10 rows. So make Grid View Paging false before For Loop and after that enable Paging of Grid View.
   
v4
Comments
RuanSmitIsNatKak 21-Jan-14 8:06am
   
I see what you are saying, but where should I do that, sorry I am a noob in vb.
RuanSmitIsNatKak 21-Jan-14 8:07am
   
or before what for loop should I make Grid View Paging false?
Er. Ajay Chauhan 21-Jan-14 8:11am
   
make GridViewName.AllowPaging = False before for loop and after that make AllowPaging True.
RuanSmitIsNatKak 21-Jan-14 8:15am
   
but at which one? rowCreated or SelectedIndexChange?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100