Gridview一列不保存数据 [英] Gridview one column not keeping data

查看:61
本文介绍了Gridview一列不保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3列的Gridview。 1)2年级年份开始3)年终结束。

第2列和第3列没问题,但第一列包含一个按钮,显示今年活跃或活跃年份(只有一个活跃的一年)。现在如果我点击让我们说第3行(让今年活跃)然后我转到网格视图的第二页然后回到一,第三行是高亮的但是活动按钮在第一行和行应显示活动年份显示活跃年份,第二页第一行显示活跃年份,但只能有一个。



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

推荐答案

你的问题是在网格视图中进行分页,无论何时执行循环,其条件只运行网格视图行计数的次数,并且网格视图行计数最大为页面大小,如果你声明页面大小只有10然后你的无论您的网格视图包含多于10行,循环都只会执行10次。因此,在For循环之前和之后使Grid View Paging为false,以启用网格视图的分页。
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.


这篇关于Gridview一列不保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆