Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
ddlDre.DataSource = dsDre
ddlDre.DataValueField = "Id_Dre"
ddlDre.DataTextField = "Nome"
ddlDre.DataBind()
ddlDre.Items.Insert(0, New ListItem("SELECIONE UMA DRE ...", ""))
Else
'PopulateCheckedValues()
'SaveCheckedValues()
'PopulateCheckedValues()
End If
End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "MouseOver(this);"
e.Row.Attributes("onmouseleave") = "MouseLeave(this);"
End If
PopulateCheckedValues()
End Sub
Protected Sub gdvEscolas_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
SaveCheckedValues()
gdvEscolas.PageIndex = e.NewPageIndex
ListarEscola()
'PopulateCheckedValues()
End Sub
Private Sub PopulateCheckedValues()
Dim userdetails As ArrayList = CType(ViewState("CHECKED_ITEMS"), ArrayList)
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
For Each gvrow As GridViewRow In gdvEscolas.Rows
Dim index As Integer = CInt(gdvEscolas.DataKeys(gvrow.RowIndex).Value)
If userdetails.Contains(index) Then
Dim myCheckBox As CheckBox = CType(gvrow.FindControl("chkEscola"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub
Private Sub SaveCheckedValues()
Dim userdetails As ArrayList = New ArrayList()
Dim index As Integer = -1
For Each gvrow As GridViewRow In gdvEscolas.Rows
index = CInt(gdvEscolas.DataKeys(gvrow.RowIndex).Value)
Dim result As Boolean = (CType(gvrow.FindControl("chkEscola"), CheckBox)).Checked
If ViewState("CHECKED_ITEMS") IsNot Nothing Then userdetails = CType(ViewState("CHECKED_ITEMS"), ArrayList)
If result Then
If Not userdetails.Contains(index) Then userdetails.Add(index)
Else
userdetails.Remove(index)
End If
Next
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then ViewState("CHECKED_ITEMS") = userdetails
End Sub
Sub Inserir_Supervisor_Escola()
Dim conexao As SqlConnection
Dim cmd As SqlCommand
Dim sql As String
SaveCheckedValues()
gdvEscolas.AllowPaging = False
ListarEscola()
For Each row2 As GridViewRow In gdvSupervisores.Rows
Dim checkbox As CheckBox = CType(row2.FindControl("chk"), CheckBox)
For Each row As GridViewRow In gdvEscolas.Rows
Dim checkbox2 As CheckBox = CType(row.FindControl("chkEscola"), CheckBox)
If checkbox.Checked And checkbox2.Checked Then
sql = "Insert Into pr_Supervisor_Escola (Matricula, Id_Escola, Id_Dre, DataCadastro, CadastradoPor) values (" & gdvSupervisores.Rows(row2.RowIndex).Cells(3).Text & ", " & gdvEscolas.Rows(row.RowIndex).Cells(1).Text & ", '" & ddlDre.SelectedValue & "', '" & Date.Now & "', '" & User.Identity.Name & "')"
Try
conexao = New SqlConnection(strConn)
conexao.Open()
cmd = New SqlCommand(sql, conexao)
cmd.ExecuteNonQuery()
conexao.Close()
gdvEscolas.UseAccessibleHeader = True
gdvEscolas.HeaderRow.TableSection = TableRowSection.TableHeader
pnlMensagemErro.Visible = False
pnlMensagemSucesso.Visible = True
Catch
pnlMensagemErro.Visible = True
lblMensagemErro.Visible = True
lblMensagemErro.Text = "Ocorreu um erro!" + Err.Description
End Try
End If
Next
Next row2
End Sub