Solved

loop through checkbox's in a datagridview on an asp.net applicaiton c# or vb.net

Posted on 2008-10-10
15
965 Views
Last Modified: 2013-11-26
OK, i have two issues

1. For some reason my checkbox is not appearing in the grid.
2. Once the checkbox appears how do i loop through all itmes in teh grid and check the status of the checkbox.

The datasource does not have a Boolean field bound to the grid.
 
Dim sql As String = _
            "Select LastName, FirstName, EmailAddress FROM RegisteredUsers"
        cmdSQL.CommandText = sql
 
        Dim dsRegisteredUsers As DataSet = objData.QueryDataset(cmdSQL, "RegisteredUsers")
'Query dataset is a function that gets my dataset based off the query
 
 
        Me.GridView1.DataSource = dsRegisteredUsers
        Me.GridView1.DataMember = "RegisteredUsers"
        Me.GridView1.DataBind()
 
 
'' Here is my markup
 
                <asp:GridView  
                    ID="GridView1" runat="server"
                    BackColor="#99FF99" BorderColor="#000066">
                        <Columns>
                         <asp:CheckBoxField HeaderText="Select" />
                         <asp:BoundField DataField="LastName" HeaderText="Last Name" 
                                SortExpression="LastName" >
                                <ItemStyle Width="120px" />
                          </asp:BoundField> 
                          <asp:BoundField DataField="FirstName" HeaderText="First Name" 
                                SortExpression="FirstName" >
                                <ItemStyle Width="120px" />
                          </asp:BoundField> 
                          <asp:BoundField DataField="EmailAddress" HeaderText="Email Address" 
                                SortExpression="EmailAddress" >
                                <ItemStyle Width="200px" />
                          </asp:BoundField>        
                        </Columns>
                     <HeaderStyle BackColor="#3399FF" />
                     <AlternatingRowStyle BackColor="#FFFF99" />
                </asp:GridView>

Open in new window

0
Comment
Question by:talker2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 4
15 Comments
 
LVL 3

Expert Comment

by:3abqari
ID: 22690421
Have you tried to use the ItemDataBound Event of the gridview?
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 22690475
and try this for the checkbox
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>&nbsp;
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 22690480
oops should be all this
<asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chk" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:burakiewicz
ID: 22690517
and for the loop
For Each gvr As GridViewRow In GridView1.Rows

Next
0
 
LVL 3

Expert Comment

by:3abqari
ID: 22690538
You don't have to use the Loop if you use the ItemDataBound Event.  The code in the ItemDataBound Event gets executed for each row of the grid.

The loop would be useful outside of the ItemDataBound Event.  I just wanted to clear that up.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 22690580
3abqari:
probably should have mentioned that the loop would just be used for processing only after the ItemDataBound Event.
0
 
LVL 7

Author Comment

by:talker2004
ID: 22690618


i am real close, got the loop like you said
but i am not able to read the value of ?gvr.Cells(0)

how can you tell if the checkbox is checked?

0
 
LVL 3

Expert Comment

by:3abqari
ID: 22690637
Try using the gvr.FindControl("chk")
0
 
LVL 3

Expert Comment

by:3abqari
ID: 22690643
or ctype(gvr.FindControl("chk"), checkbox).checked
0
 
LVL 7

Author Comment

by:talker2004
ID: 22690700
sweet it looks good i am going to try it when i get home.

Is it just me or is ee having some issues with speed today?

0
 
LVL 3

Expert Comment

by:3abqari
ID: 22690731
I'm using the Expert skin, which eliminates all the advertisement that are in the Premium skin...  It's fine here.
0
 
LVL 7

Author Comment

by:talker2004
ID: 22691990
is there any reason it would be coming up false when it's checked

and after the post backs all of my checks clear
0
 
LVL 3

Expert Comment

by:3abqari
ID: 22692074
tell me exactly what you are trying to do with this grid so that I can maybe answer you?
0
 
LVL 7

Author Comment

by:talker2004
ID: 22692092
duh, i was binding the grid in the page load event and i did not have an IsPostBack condition.

it's working now, thanks so much.....
0
 
LVL 3

Accepted Solution

by:
3abqari earned 500 total points
ID: 22692133
Yeah, the Page.IsPostBack is something that you have to keep track of in web development...
0

Featured Post

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question