Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a gridview i can get the total records count by
below code
GridView1.Rows.count
but in my grid i have check boxes and i have a button out side the grid. when i click on the button i want to get the count of only selected rows with checkboxes in the grid. how can we do that?

Thanks in Advance
Posted

C#
protected void Button2_Click(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                //finding checkbox in GridView
                CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                //CheckBox not null
                if (cbx != null)
                {
                    //if CheckBox Checked
                    if (cbx.Checked)
                    {
                        //add your logic here.
                        //this is for example to get first column values of selected CheckBox
                        string firstColumnValue = GridView1.Rows[i].Cells[0].Text;
                        Response.Write(firstColumnValue);
                    }
                }
            }
        }
    }
 
Share this answer
 
Comments
Sandeep Mewara 29-Jul-10 11:59am    
Comment from OP:
Hi makhaai,

thanks for your answer.. is there a way to check this count on client side using javascript?
raju melveetilpurayil 29-Jul-10 12:32pm    
Actually I don't know how to do, But i think its difficult to loop in html. if you can please provide some links, my net connection is dead slow. thats will helpful to me and yadlaprasad.
raju melveetilpurayil 29-Jul-10 12:44pm    
this link will help http://stackoverflow.com/questions/228549/get-gridview-selected-row-datakey-in-javascript
thanks to all.
You need to loop through all the rows of the Grid.
1. Find the checkbox control in each row
2. Find the state of checkbox. If chekced then increment the counter.

Once loop ends, show the count as selected number of rows.
 
Share this answer
 
Comments
koool.kabeer 29-Jul-10 11:43am    
yeah looping through the rows of gridview
Create a public Static method in YourForm.aspx.cs
C#
public static String CreateCheckBox(Int32 index)
{
    ///Return a checkbox created with dynamic ID
    return "<input type="\"checkbox\"" id="\"Chk_"" index="" hold=" /> " onclick="\"checkSelection()\"" />";
}


Create a item template in gridView... like this
HTML
<TemplateField HeaderText="">
<ItemTemplate HorizontalAlign="Left" />
<ItemTemplate>
<%#YourNamespace.YourForm.CreateCheckBox(Container.DisplayIndex)%>
</ItemTemplate>
</TemplateField>


Add the javascript function (On click) of the checkboxes
JavaScript
function checkSelection() {
var nMarked = 0;
for (var i = 0; i < document.forms[0].elements.length; i++) {
if (document.forms[0].elements[i].checked &&
document.forms[0].elements[i].name.indexOf('Chk_') > -1) {
    nMarked = nMarked + 1;
}
}
if (nMarked == 0) {
    alert("no element has been selected");
    return;
 }
else
{
    alert("You have selected " + nMarked + " checkboxes");
    //Do other stuff here
}
}


tell me what hapened ;)
 
Share this answer
 
Hi makhaai,

thanks for your answer.. is there a way to check this count on client side using javascript?
 
Share this answer
 
Comments
AspDotNetDev 29-Jul-10 11:57am    
You can get the client ID of a control using control.ClientID. You can then use Javascript to find those controls by their ID and check if they are checked.
Sandeep Mewara 29-Jul-10 11:59am    
Reason for my vote of 1
Not an answer. Should be a Comment to the answer you want to interact.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900