Last active
April 5, 2017 21:16
-
-
Save ycaroafonso/67548f2b3cf7c59aa3bc59d47f4b032e to your computer and use it in GitHub Desktop.
GridView paginada com checkbox em VB.Net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Data | |
Imports System.Data.SqlClient | |
Partial Class _Default | |
Inherits System.Web.UI.Page | |
''' | |
''' Armazena as ID's em uma viewstate | |
''' | |
''' | |
''' | |
''' | |
Public Property lstIdChkChecados() As List(Of Integer) | |
Get | |
If ViewState("lstIdChkChecados") Is Nothing Then ViewState("lstIdChkChecados") = New List(Of Integer) | |
Return ViewState("lstIdChkChecados") | |
End Get | |
Set(ByVal value As List(Of Integer)) | |
ViewState("lstIdChkChecados") = value | |
End Set | |
End Property | |
''' | |
''' atualiza o viewstate com as ID's da DataKey (DataKeyNames) da gridview. | |
''' | |
''' | |
Sub AtualizaViewStateChkChecados() | |
Dim Chk As CheckBox | |
For Each row As GridViewRow In GridView1.Rows | |
Chk = DirectCast(row.FindControl("CheckBox1"), CheckBox) | |
If Chk.Checked Then | |
If Not lstIdChkChecados.Contains(GridView1.DataKeys(row.RowIndex)(0)) Then | |
lstIdChkChecados.Add(GridView1.DataKeys(row.RowIndex)(0)) | |
End If | |
Else | |
If lstIdChkChecados.Contains(GridView1.DataKeys(row.RowIndex)(0)) Then | |
lstIdChkChecados.Remove(GridView1.DataKeys(row.RowIndex)(0)) | |
End If | |
End If | |
Next | |
End Sub | |
''' | |
''' Quando clica para mudar de página, é executado o método AtualizaViewStateChkChecados() antes de atualizar o GridView. | |
''' | |
''' | |
''' | |
''' | |
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging | |
AtualizaViewStateChkChecados() | |
End Sub | |
''' | |
''' Depois que ocorre a mudança, verifica se na página atual da GridView existe checkbox marcados. | |
''' | |
''' | |
''' | |
''' | |
Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender | |
If Not IsPostBack Then | |
For Each row As DataRowView In SqlDataSource1.Select(DataSourceSelectArguments.Empty) | |
If row("Ativo") Then | |
lstIdChkChecados.Add(row("ID")) | |
End If | |
Next | |
End If | |
For Each row As GridViewRow In GridView1.Rows | |
DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked = lstIdChkChecados.Contains(GridView1.DataKeys(row.RowIndex)(0)) | |
Next | |
End Sub | |
''' | |
''' Atualiza a tabela do banco de dados usando lstIdChkChecados | |
''' | |
''' | |
''' | |
''' | |
Protected Sub btnAtualizar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAtualizar.Click | |
AtualizaViewStateChkChecados() | |
Dim conexao As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnectionStringSqlServer").ConnectionString) | |
Try | |
' Primeiro atualiza todos os registros, passando o campo Ativo para FALSE | |
' Se for usar em uma tabela com muitos registros, deve arrumar uma solução para um melhor desempenho | |
conexao.Open() | |
Dim comando As New SqlCommand | |
comando.Parameters.Add("@Ativo", SqlDbType.Bit).Value = False | |
comando.CommandText = "UPDATE DotNet_GridViewPaginadaComCheckbox SET Ativo = @Ativo" | |
comando.CommandType = CommandType.Text | |
comando.Connection = conexao | |
comando.ExecuteNonQuery() | |
' Antes de fechar a conexão, passa por todos os itens do lstIdChkChecados e atualiza no banco, passando o campo Ativo para true. | |
comando.CommandText = "UPDATE DotNet_GridViewPaginadaComCheckbox SET Ativo = @Ativo WHERE ID = @ID" | |
comando.Parameters(0).Value = True | |
comando.Parameters.Add("@ID", SqlDbType.Int) | |
For Each item As Integer In lstIdChkChecados | |
comando.Parameters(1).Value = item | |
comando.ExecuteNonQuery() | |
Next | |
Catch ex As Exception | |
Finally | |
conexao.Close() | |
End Try | |
End Sub | |
End Class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment