点击“更新”按钮后,它会将gridview数据更新为数据库。
但是gridview数据无法自动刷新,它仍然会在修改前显示数据。
我需要再次返回页面才能看到更新的数据。有什么问题 ?更新后我已经绑定了数据......
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
乔
答案 0 :(得分:1)
在我的情况下,我无法重置编辑索引。
虽然看起来Op的代码已经包含了这个,但是对于像我一样到达此页面的其他人,只需确保在数据绑定之前重置EditIndex
。
Gridview1.EditIndex = -1
Gridview1.DataBind()
答案 1 :(得分:0)
您正在尝试绑定RowUpdating事件中的数据。单击行的“更新”按钮时,但在GridView控件更新行之前,将引发RowUpdating事件。
所以我认为你可以绑定RowUpdated事件中的数据。单击行的“更新”按钮但在GridView控件更新行之后引发RowUpdated事件。