how to count number of unchecked checkboxes in a gridview?

  • Thread starter Thread starter Dan
  • Start date Start date
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>
 
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
 
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
 
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
 
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
 
Thanks, this works ... if i define x as Friend and not as Dim, because
otherwise, x is still 0 or 1.

"Alexey Smirnov" <[email protected]> schreef in bericht
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
 
Thanks, this works ... if i define x as Friend and not as Dim, because
otherwise, x is still 0 or 1.

"Alexey Smirnov" <[email protected]> schreef in bericht
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
 
Back
Top