3 Replies - 1120 Views - Last Post: 18 February 2015 - 01:48 PM

#1 EliteFord  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 09-September 14

Uncheck/Check all checkboxes using Datagrid

Posted 17 February 2015 - 02:51 PM

Hi Guys,

I am trying to get a checkbox to check all of the other checkboxes down the line on and off.
I have researched methods on the internet but most of them are using DataGridView and I am using a DataGrid.

I was wondering if someone could help me with the VB code for checking all the boxes and unchecking them.
I found something like this:
 Protected Sub ChkCheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkCheckAll As CheckBox = CType(sender, CheckBox)

        For RowCount As Integer = 0 To DataGrid1.Items.Count - 1
            CType(DataGrid1.Items(RowCount).Cells(0).FindControl("RcvChkBox"), CheckBox).Checked = chkCheckAll.Checked
        Next
    End Sub



Which seemed to work for the user - however when I tried implementing this method into my web form it doesn't select all the checkboxes. It will uncheck them all if you manually check them but not check them all.
I have tried other methods as well but I cant seem to figure out. Like I have said, everything I look up seems to be using a DataGridView instead of just DataGrid.

HTML
<asp:TableRow>
        <asp:TableCell>
             <asp:DataGrid ID="DataGrid2" AllowSorting="true" runat="server" CellSpacing="0" CellPadding="30" Width="890px" AutoGenerateColumns="False" GridLines="Both" >
                <Columns>
                <asp:TemplateColumn HeaderText="Registered" HeaderStyle-Font-Size="Large"><HeaderTemplate><center><asp:CheckBox ID="rgstrChkAll" AutoPostBack="true" OnCheckedChanged="DistrChkCheckAll_CheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' Text="<br/>Select All" runat="server" /></center></HeaderTemplate>
                    <ItemTemplate>
                    <center><asp:CheckBox ID="rgstrChkBox" runat="server" /></center>
                    </ItemTemplate>
                </asp:TemplateColumn>

                <asp:TemplateColumn HeaderText="Serial Number #" HeaderStyle-Font-Size="Large" HeaderStyle-Width="695px">
                   <ItemTemplate>
                   <asp:Label ID="serialNum" runat="server" Text='<%# Bind("AddressID") %>'></asp:Label><br />
                   </ItemTemplate>
                </asp:TemplateColumn>
                </Columns>

             </asp:DataGrid>            
        </asp:TableCell>
    </asp:TableRow>



VB
Protected Sub ChkCheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkCheckAll As CheckBox = CType(sender, CheckBox)

        For RowCount As Integer = 0 To DataGrid1.Items.Count - 1
            CType(DataGrid1.Items(RowCount).Cells(0).FindControl("RcvChkBox"), CheckBox).Checked = chkCheckAll.Checked
        Next
    End Sub



Much appreciated for any info. Thanks guys

Is This A Good Question/Topic? 0
  • +

Replies To: Uncheck/Check all checkboxes using Datagrid

#2 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,909
  • Joined: 12-December 12

Re: Uncheck/Check all checkboxes using Datagrid

Posted 17 February 2015 - 02:58 PM

Moved to ASP.NET forum.
Was This Post Helpful? 0
  • +
  • -

#3 djjeavons  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Uncheck/Check all checkboxes using Datagrid

Posted 18 February 2015 - 01:46 AM

Hi

There are a few issues with the code that needed to be fixed up before I could test it. I don't know if these were typos (in which case, please copy and paste the code that you are using to avoid this) or if you have tried so many different things that your code has become a little messy.

Firstly, you are looping through the rows of DataGrid1 but your DataGrid is called DataGrid2. Secondly, you are looking for a check box called RcvChkBox but it is called rgstrChkBox. And finally, you are binding to an event called DistrChkCheckAll_CheckedChanged but your handler in the code behind is called ChkCheckAll_CheckedChanged.

Fixing these pieces up worked fine. However, I wonder how you are binding the data to your DataGrid as if you are binding it each time the form is loaded without checking first if the reason for the form loading is via a post back then you will always end up with the check boxes in a non checked state.

So for example, your page load binding (if you are indeed binding on page load) would be something like:

If Not Page.IsPostBack Then
    DataGrid2.DataSource = whatever your data source is
    DataGrid2.DataBind()
End If



So the code I ended up with is as follows without the binding code:

Code behind
    Protected Sub DistrChkCheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkCheckAll As CheckBox = CType(sender, CheckBox)

        For RowCount As Integer = 0 To DataGrid2.Items.Count - 1
            CType(DataGrid2.Items(RowCount).Cells(0).FindControl("rgstrChkBox"), CheckBox).Checked = chkCheckAll.Checked
        Next
    End Sub



Designer file
            <asp:TableRow>
                <asp:TableCell>
                    <asp:DataGrid ID="DataGrid2" AllowSorting="true" runat="server" CellSpacing="0" CellPadding="30" Width="890px" AutoGenerateColumns="False" GridLines="Both">
                        <Columns>
                            <asp:TemplateColumn HeaderText="Registered" HeaderStyle-Font-Size="Large">
                                <HeaderTemplate>
                                    <center><asp:CheckBox ID="rgstrChkAll" AutoPostBack="true" OnCheckedChanged="DistrChkCheckAll_CheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' Text="<br/>Select All" runat="server" /></center>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <center><asp:CheckBox ID="rgstrChkBox" runat="server" /></center>
                                </ItemTemplate>
                            </asp:TemplateColumn>

                            <asp:TemplateColumn HeaderText="Serial Number #" HeaderStyle-Font-Size="Large" HeaderStyle-Width="695px">
                                <ItemTemplate>
                                    <asp:Label ID="serialNum" runat="server" Text='<%# Bind("AddressID") %>'></asp:Label><br />
                                </ItemTemplate>
                            </asp:TemplateColumn>
                        </Columns>
                        
                    </asp:DataGrid>
                </asp:TableCell>
            </asp:TableRow>



Also, another approach to this would be to use Javascript so that you do not need to post back to the server to perform the check all/uncheck all. This would be more appropriate as it is client side behaviour that you are trying to implement and not really something that the server needs to deal with.

HTH

This post has been edited by djjeavons: 18 February 2015 - 01:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 EliteFord  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 09-September 14

Re: Uncheck/Check all checkboxes using Datagrid

Posted 18 February 2015 - 01:48 PM

Djjeavons,

Thank you for the reply.

First of all - The reason there were different tags all over the place was because I didn't realize I had used the wrong DataGrid. That's my fault as I didn't proofread before posting. So sorry about that.

Second - I have two DataGrid's in my code. Datagrid1 & Datagrid2. Both of them are essentially tables that will hold different columns and different values. So DG1 will have a line#, Qty, and Product#. And DG2 will have a serial#.
In DG1 - these values are going to be coming from a server through a SQL connection which I already have in place.
In DG2 - these value(s) will be coming from a database.

Both DataGrids will need to have the check all/uncheck all function for printing and service purposes (printing because one side will be used to print labels and the other side will be used to send to our service department).

Now I have tried what you have posted within my code and I still cannot get it to work. I tried adding a Postback feature like you had mentioned but that didn't seem to do anything.
The check box is basically going to be for the user and which label they are going to print.


The web app is broken down into two parts - one for our Receiving side and one for our Distribution side.

Synopsis:
(RECEIVING) - The user will search for an activation code. When they find the one they want they will have an email that they will have to enter that has the activation code in it followed by any additional notes they want to add. Then they will send it and save it to a database.
If there activation code is a PO# then they will select the label(s) they want from the first datagrid (DataGrid1) and print them.

(DISTRIBUTION) - The user will look up the activation code from the printed label and they will see the URL from the email that the previous user used along with any additional notes that might have been added. If there are problems then they have a button that will send all of that information to our service department.
Their datagrid shows serial numbers. They will select whichever and how many ever numbers they need and send it to service.

Question:

Now I am not sure entirely on whether or not we need the binding to work - but all I need is for the checkboxes to select all or deselect all so that the users can quickly print or send their labels.

Problem:
In theory, the code should work. However it does not and instead if I hit select all - it just checks real fast and then refreshes due to the postback. But if I manually select them all and then hit the select all box it works and deselects them all.

I am not sure what the issue is. I hope this sort of helps.

DataGrid2:
<asp:TableRow>
        <asp:TableCell>
             <asp:DataGrid ID="DataGrid2" AllowSorting="true" runat="server" CellSpacing="0" CellPadding="30" Width="890px" AutoGenerateColumns="False" GridLines="Both" >
                <Columns>
                <asp:TemplateColumn HeaderText="Registered" HeaderStyle-Font-Size="Large">
                    <HeaderTemplate>
                        <center><asp:CheckBox ID="rgstrChkAll" AutoPostBack="true" OnCheckedChanged="DistrChkCheckAll_CheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' Text="<br/>Select All" runat="server" /></center>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <center><asp:CheckBox ID="rgstrChkBox" runat="server" /></center>
                    </ItemTemplate>
                </asp:TemplateColumn>

                <asp:TemplateColumn HeaderText="Serial Number #" HeaderStyle-Font-Size="Large" HeaderStyle-Width="695px">
                   <ItemTemplate>
                   <asp:Label ID="serialNum" runat="server" Text='<%# Bind("AddressID") %>'></asp:Label><br />
                   </ItemTemplate>
                </asp:TemplateColumn>
                </Columns>

             </asp:DataGrid>            
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>



Back End Code:
Protected Sub DistrChkCheckAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chkCheckAll As CheckBox = CType(sender, CheckBox)
        Dim myDg As New DataGrid
        For RowCount As Integer = 0 To DataGrid2.Items.Count - 1
            CType(DataGrid2.Items(RowCount).Cells(0).FindControl("rgstrChkBox"), CheckBox).Checked = chkCheckAll.Checked
        Next
        If Not Page.IsPostBack Then
            DataGrid2.DataSource = myDg
            DataGrid2.DataBind()
        End If
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1