Joe Yan
Joe Yan

Reputation: 2095

Gridview data not refresh after rowupdating

After I click the "Update" button, it update the gridview data to database.

But the gridview data cannot auto refresh, it still show data before amendment.

I need to go back the page again to see updated data. What's the problem ? I had already Bind data after updating...

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles Gridview1.RowUpdating
    If Gridview1.EditIndex = -1 Then
        BindData()
        Exit Sub
    End If

    If e.RowIndex >= 0 Then

        Dim row As GridViewRow = DirectCast(Gridview1.Rows(e.RowIndex), GridViewRow)

        If row.RowType = DataControlRowType.DataRow Then

            Dim Col1_SL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_SL"), CheckBox)
            Dim Col1_VL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_VL"), CheckBox)
            Dim Col1_ML = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_ML"), CheckBox)
            Dim Col1_PH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_PH"), CheckBox)
            Dim Col1_APH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_APH"), CheckBox)
            Dim Col1_TOIL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_TOIL"), CheckBox)
            Dim Col1_Others = CType(Gridview1.Rows(e.RowIndex).FindControl("tb1_Others"), TextBox)
            Dim Col1_RosterKey = CType(Gridview1.Rows(e.RowIndex).FindControl("lb1_rosterkey"), Label)
            Dim Col1_StartTime = CType(Gridview1.Rows(e.RowIndex).FindControl("Col1_StartTime"), TimeSelector)
            Dim Col1_EndTime = CType(Gridview1.Rows(e.RowIndex).FindControl("Col1_EndTime"), TimeSelector)

            Dim cmd As New System.Data.SqlClient.SqlCommand
            Dim sql As String
            Dim reader As System.Data.SqlClient.SqlDataReader

            'Col1
            Using conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("hris_shiftdutyConnectionString").ConnectionString)
                conn.Open()
                cmd.Connection = conn
                sql = "SET DATEFORMAT dmy;UPDATE troster SET SL='" & Convert.ToInt32(Col1_SL.Checked) & "', VL='" & Convert.ToInt32(Col1_VL.Checked) & "', ML='" & Convert.ToInt32(Col1_ML.Checked) & "', "
                sql += "PH='" & Convert.ToInt32(Col1_PH.Checked) & "', APH='" & Convert.ToInt32(Col1_APH.Checked) & "', TOIL='" & Convert.ToInt32(Col1_TOIL.Checked) & "', "
                sql += "Others='" & RTrim(Col1_Others.Text) & "', "

                If Col1_StartTime.AmPm = TimeSelector.AmPmSpec.PM Then
                    sql += "start_time='" & Col1_StartTime.Hour + 12 & ":" & Col1_StartTime.Minute & ":00', "
                Else
                    sql += "start_time='" & Col1_StartTime.Hour & ":" & Col1_StartTime.Minute & ":00', "
                End If
                If Col1_EndTime.AmPm = TimeSelector.AmPmSpec.PM Then
                    sql += "end_time='" & Col1_EndTime.Hour + 12 & ":" & Col1_EndTime.Minute & ":00' "
                Else
                    sql += "end_time='" & Col1_EndTime.Hour & ":" & Col1_EndTime.Minute & ":00' "
                End If

                sql += "where roster_key='" & Col1_RosterKey.Text & "';"
                cmd.CommandText = sql
                reader = cmd.ExecuteReader()
                conn.Close()
                reader.Close()
            End Using

            'Reset the edit index.
            Gridview1.EditIndex = -1

            'Bind data to the GridView control.
            BindData()
        End If
    End If


End Sub

Private Sub BindData()

    Dim StartDateStr As String

    StartDateStr = Trim(Request.QueryString("sd"))
    StartDateStr = Left(StartDateStr, 10)
    'date should be best in ISO format, i.e. yyyy-mm-ddTHH:mm:ss[.mmm] as "Set Dateformat dmy" is not supported by DataSet object
    'StartDateStr = Right(StartDateStr, 4) & "-" & Mid(StartDateStr, 4, 2) & "-" & Left(StartDateStr, 2) & " T00:00:00"

    Dim StartDate As DateTime
    Dim EndDate As DateTime
    StartDate = Convert.ToDateTime(StartDateStr)
    EndDate = Format(DateAdd(DateInterval.Day, 6, StartDate), "dd/MM/yyyy")

    g_header1 = StartDate   'Monday
    g_header2 = Format(DateAdd(DateInterval.Day, 1, StartDate), "dd/MM/yyyy")
    g_header3 = Format(DateAdd(DateInterval.Day, 2, StartDate), "dd/MM/yyyy")
    g_header4 = Format(DateAdd(DateInterval.Day, 3, StartDate), "dd/MM/yyyy")
    g_header5 = Format(DateAdd(DateInterval.Day, 4, StartDate), "dd/MM/yyyy")
    g_header6 = Format(DateAdd(DateInterval.Day, 5, StartDate), "dd/MM/yyyy")
    g_header7 = EndDate     'Sunday

    Gridview1.DataSource = Session("dt")
    Gridview1.DataBind()
End Sub

Joe

Upvotes: 2

Views: 2865

Answers (2)

Zarepheth
Zarepheth

Reputation: 2603

In my case, I failed to reset the edit index. Though it appears that Op's code already includes this, for others reaching this page like I did, just make sure the EditIndex is reset before the data bind.

Gridview1.EditIndex = -1
Gridview1.DataBind()

Upvotes: 1

Saanch
Saanch

Reputation: 1844

You are trying to bind the data in the RowUpdating event. The RowUpdating event is raised when a row's Update button is clicked, but before the GridView control updates the row.

So I think you can bind the data in the RowUpdated event. The RowUpdated event is raised when a row's Update button is clicked, but after the GridView control updates the row.

Upvotes: 0

Related Questions