DarkAngelRev Posted July 3, 2012 at 07:57 PM Report Share #467102 Posted July 3, 2012 at 07:57 PM boas... estou encalhado... tenho um grid view que faz o select das linhas através de checkbox, está práticamente funcional, tem um pequeno bug que não consigo solucionar... quando carrego no quadradinho de seleccionar todos ele faz tudo bem... mas se des-seleccionar só um elemento e depois voltar a seleccionar ele não volta a fazer o check da checkbox de seleccionar tudo e pior entra na condição (deixo o código em baixo) onde faz o uncheck all... se alguém puder ajudar agradecia... deixo o código: If IsPostBack Then If VerificaCabecalho(gvPrimavera, "checkAll") = True Then Dim n As Integer = ContarSeleccionados(gvPrimavera, "checkSelection") If n = 10 Or n = 0 Then CheckState(True, gvPrimavera, "checkSelection") SeleccionarChecked(gvPrimavera, "checkSelection") Else Dim cb2 As CheckBox = gvPrimavera.HeaderRow.FindControl("checkAll") cb2.Checked = False SeleccionarChecked(gvPrimavera, "checkSelection") End If Else Dim n As Integer = ContarSeleccionados(gvPrimavera, "checkSelection") If n = 0 Then 'se nenhum está seleccionado UncheckAll(gvPrimavera, "checkSelection", "checkAll") If VerificaCabecalho(gvPrimavera, "checkAll") = True Then Dim cb As CheckBox = gvPrimavera.HeaderRow.FindControl("checkAll") cb.Checked = False End If ElseIf n = 10 Then 'se estão todos seleccionados If VerificaCabecalho(gvPrimavera, "checkAll") = False Then UncheckAll(gvPrimavera, "checkSelection", "checkAll") Else Dim cb As CheckBox = gvPrimavera.HeaderRow.FindControl("checkAll") cb.Checked = True End If Else 'se estao alguns seleccionados SeleccionarChecked(gvPrimavera, "checkSelection") End If End If If VerificaCabecalho(gvCliente, "checkAll2") = True Then Dim n As Integer = ContarSeleccionados(gvCliente, "checkSelection2") If n = 10 Or n = 0 Then CheckState(True, gvCliente, "checkSelection2") SeleccionarChecked(gvCliente, "checkSelection2") Else Dim cb2 As CheckBox = gvCliente.HeaderRow.FindControl("checkAll2") cb2.Checked = False SeleccionarChecked(gvCliente, "checkSelection2") End If Else Dim n As Integer = ContarSeleccionados(gvCliente, "checkSelection2") If n = 0 Then 'se nenhum está seleccionado UncheckAll(gvCliente, "checkSelection2", "checkAll2") If VerificaCabecalho(gvCliente, "checkAll2") = True Then Dim cb As CheckBox = gvCliente.HeaderRow.FindControl("checkAll2") cb.Checked = False End If ElseIf n = 10 Then 'se estão todos seleccionados If VerificaCabecalho(gvCliente, "checkAll2") = False Then UncheckAll(gvCliente, "checkSelection2", "checkAll2") Else Dim cb As CheckBox = gvCliente.HeaderRow.FindControl("checkAll2") cb.Checked = True End If Else 'se estao alguns seleccionados SeleccionarChecked(gvCliente, "checkSelection2") End If End If end sub as outras funções invocadas em cima caso achem que o problema está nelas... o n é um contador sendo que 10 é o número máximo de linhas da gridview... Private Sub SeleccionarChecked(ByVal Grid As GridView, ByVal controlo As String) For Each row As GridViewRow In Grid.Rows Dim cb As CheckBox = row.FindControl(controlo) If cb.Checked = True Then row.RowState = DataControlRowState.Selected End If Next End Sub Private Function ContarSeleccionados(ByVal grid As GridView, ByVal controlo As String) As Integer Dim n As Integer = 0 For Each row As GridViewRow In grid.Rows Dim cb As CheckBox = row.FindControl(controlo) If cb.Checked = True Then n += 1 End If Next Return n End Function Private Function VerificaCabecalho(ByVal Grid As GridView, ByVal controlo As String) As Boolean 'Função que verifica se o cabeçalho de um gridview está seleccionado ou não Dim cb As CheckBox = Grid.HeaderRow.FindControl(controlo) Return cb.Checked End Function Private Function CheckState(ByVal chckState As Boolean, ByVal grid As GridView, ByVal controlname As String) As Integer() ' Por cada linha do grid vai verificar quais linhas têm as checkbox checkada Dim n As Integer() = {0, 0} For Each row As GridViewRow In grid.Rows Dim cb As CheckBox = row.FindControl(controlname) If cb IsNot Nothing And chckState = True Then cb.Checked = chckState n(0) += 1 ElseIf cb IsNot Nothing And chckState = False Then cb.Checked = chckState n(1) += 1 End If Next Return n End Function Protected Sub UncheckAll(ByVal grid As GridView, ByVal controloLinhas As String, ByVal controloHeader As String) Dim cb As CheckBox = grid.HeaderRow.FindControl(controloHeader) If cb.Checked = False Then CheckState(False, grid, controloLinhas) End If End Sub se alguém puder ajudar agradeço imenso... Link to comment Share on other sites More sharing options...
jlpcalado Posted July 3, 2012 at 10:24 PM Report Share #467153 Posted July 3, 2012 at 10:24 PM Se estou a perceber tens uma checkbox no cabeçalho da grid e que ao checar coloque todos os items da grid checados, e vice-versa. Estas 2 (?) ações devem ser independentes do checar ou não cada item de per si, não é? Então bastará criares um evento para suportar essas ações. Exemplo: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="projecto_id" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="CheckAll" OnCheckedChanged="CheckAll_OnCheckedChanged" AutoPostBack="true" runat="server" /> Check All </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="projecto_id" HeaderText="projecto_id" ReadOnly="True" SortExpression="projecto_id" /> <asp:BoundField DataField="processo" HeaderText="processo" SortExpression="processo" /> </Columns> </asp:GridView> Protected Sub CheckAll_OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) For i = 0 To GridView1.Rows.Count - 1 Dim cb As CheckBox = GridView1.Rows(i).FindControl("CheckBox1") cb.Checked = sender.checked Next End Sub 1 Report Link to comment Share on other sites More sharing options...
DarkAngelRev Posted July 3, 2012 at 11:18 PM Author Report Share #467167 Posted July 3, 2012 at 11:18 PM Se estou a perceber tens uma checkbox no cabeçalho da grid e que ao checar coloque todos os items da grid checados, e vice-versa. Estas 2 (?) ações devem ser independentes do checar ou não cada item de per si, não é? Então bastará criares um evento para suportar essas ações. Exemplo: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="projecto_id" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="CheckAll" OnCheckedChanged="CheckAll_OnCheckedChanged" AutoPostBack="true" runat="server" /> Check All </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="projecto_id" HeaderText="projecto_id" ReadOnly="True" SortExpression="projecto_id" /> <asp:BoundField DataField="processo" HeaderText="processo" SortExpression="processo" /> </Columns> </asp:GridView> Protected Sub CheckAll_OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs) For i = 0 To GridView1.Rows.Count - 1 Dim cb As CheckBox = GridView1.Rows(i).FindControl("CheckBox1") cb.Checked = sender.checked Next End Sub pois eu estava a fazer junto... mas se calhar tens razão... vou adoptar a tua sugestão e depois dou feedback... 😉 obrigado Link to comment Share on other sites More sharing options...
DarkAngelRev Posted July 3, 2012 at 11:37 PM Author Report Share #467171 Posted July 3, 2012 at 11:37 PM boas, isso resolveu o problema... estava a querer fazer tudo duma vez e acabei por complicar o que era simples... muito obrigado... abraço Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now