how to count number of unchecked checkboxes in a gridview?

D

Dan

Hi,

in this gridview, there is in each row a checkbox linked to a field in the
database. Some are checked, other not. I want to know how many are (e.g.)
unchecked when the gridview is loaded.
This is my attempt (vb.net), but it doesn't work.
Thanks for help.
Dan.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
dim x as integer = 0
If e.Row.RowType = DataControlRowType.DataRow Then
if e.Row.Cells(2).Text = "false" then x=x+1
response.write("number of unchecked checkbox:" & x)
end if
end Sub

asp:GridView ID="GridView1" runat="server" />
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />
<asp:TemplateField HeaderText="selected" >
<ItemTemplate>
<asp:CheckBox ID="chb" runat="server" Checked='<%# Bind("selct") %>' >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
G

Guest

Hi,

in this gridview, there is in each row a checkbox linked to a field in the
database. Some are checked, other not. I want to know how many are (e.g.)
unchecked when the gridview is loaded.
This is my attempt (vb.net), but it doesn't work.
Thanks for help.
Dan.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
dim x as integer = 0
If e.Row.RowType = DataControlRowType.DataRow Then
if e.Row.Cells(2).Text = "false" then x=x+1
response.write("number of unchecked checkbox:" & x)
end if
end Sub

asp:GridView ID="GridView1" runat="server" />
<Columns>
<asp:CommandField  ShowSelectButton="true" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />
<asp:TemplateField HeaderText="selected" >
<ItemTemplate>
<asp:CheckBox ID="chb" runat="server" Checked='<%# Bind("selct") %>' >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I am not sure but it seems if you don't set the Text property to chb,
you can't get it. Use following

If e.Row.RowType = DataControlRowType.DataRow Then
if e.Row.DataItem("selct") = "false" then x=x+1

This should work
 
M

Mr. Arnold

Dan said:
Hi,

in this gridview, there is in each row a checkbox linked to a field in the
database. Some are checked, other not. I want to know how many are (e.g.)
unchecked when the gridview is loaded.
This is my attempt (vb.net), but it doesn't work.
Thanks for help.
Dan.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
dim x as integer = 0
If e.Row.RowType = DataControlRowType.DataRow Then
if e.Row.Cells(2).Text = "false" then x=x+1
response.write("number of unchecked checkbox:" & x)
end if
end Sub


Maybe, you need to try something different by using the example in the link
and using the second example of searching the grid, which I don't see why
you couldn't tweak the code to come back with a count of unchecked data data
in a column. The routine could be executed right after the load of the grid.

http://www.automatedqa.com/communit...leType/ArticleView/articleId/502/Default.aspx


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4101 (20090525) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
D

Dan

Thanks, this works ... if i define x as Friend and not as Dim, because
otherwise, x is still 0 or 1.

Hi,

in this gridview, there is in each row a checkbox linked to a field in the
database. Some are checked, other not. I want to know how many are (e.g.)
unchecked when the gridview is loaded.
This is my attempt (vb.net), but it doesn't work.
Thanks for help.
Dan.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
dim x as integer = 0
If e.Row.RowType = DataControlRowType.DataRow Then
if e.Row.Cells(2).Text = "false" then x=x+1
response.write("number of unchecked checkbox:" & x)
end if
end Sub

asp:GridView ID="GridView1" runat="server" />
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />
<asp:TemplateField HeaderText="selected" >
<ItemTemplate>
<asp:CheckBox ID="chb" runat="server" Checked='<%# Bind("selct") %>' >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I am not sure but it seems if you don't set the Text property to chb,
you can't get it. Use following

If e.Row.RowType = DataControlRowType.DataRow Then
if e.Row.DataItem("selct") = "false" then x=x+1

This should work
 
Last edited by a moderator:
Joined
Mar 4, 2009
Messages
17
Reaction score
0
I THINK ROWDATABOUND IS NOT APPROPRIATE. WHY YOU DON'T TRY THROUGH A FOR EACH LOOP AFTER BIND?

REGARDS
shawpnendu.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top