Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to count no of rows i checked in gridview. I had taken checkbox in gridview
thanks
Posted

C#
protected void btnCountSelectedRows_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);
                    }
                }
            }
        }
    }


Try This one using JavaScript:
Create a public Static method in YourForm.aspx.cs
public static String CreateCheckBox(Int32 index)
{
    ///Return a checkbox created with dynamic ID
    return "<input type="\"checkbox\"" id="\"Chk_"" index="" hold=" /> "  önclick="\"checkSelection()\"" />";
}

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

Add the javascript function (On click) of the checkboxes
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
   
v2
Comments
SantoshRohinSantosh 25-Jan-12 1:35am
   
This method is not giving the exact count .It is giving me 0 count .
Prasad_Kulkarni 25-Jan-12 1:43am
   
I have used that method in my project its working fine,
let it be just try with second method i have posted.
SantoshRohinSantosh 25-Jan-12 1:47am
   
Thanks pashad but i am not using aspx page i am doing standalone project so please help me in doing that.
Prasad_Kulkarni 25-Jan-12 1:52am
   
For standalone, the first solution was exact one, see if you are missing out something,n let me know i'm trying my best to give you what you want okay.
If you can just post your code, so i'll come to know what you did n what more you require.
SantoshRohinSantosh 25-Jan-12 2:12am
   
I got the solution,it is like this
foreach (DataGridViewRow row in datagridallstudents.Rows)
{

DataGridViewCheckBoxCell cell = row.Cells["SELECT"] as DataGridViewCheckBoxCell;
//select is the header name.
ClsConnection.Conn.Close();

if (cell.Value != null)
{
MessageBox.Show(row.Cells[0].Value.ToString());
}

Thanks Pashad
Prasad_Kulkarni 25-Jan-12 5:39am
   
You're welcome
Declare a global variable count.
Just in GridView1_RowDataBound event, find your row which status is checked and increase it by 1 Like(count+=1)..

And in some other events like button click show it...
   
v2
Comments
SantoshRohinSantosh 25-Jan-12 1:33am
   
Thanks Amit but there is no such event GridView1_RowDataBound
Can u tell me some other mehtod to do it.
C#
foreach (DataGridViewRow row in datagridallstudents.Rows)
                    {

                        DataGridViewCheckBoxCell cell = row.Cells["SELECT"] as DataGridViewCheckBoxCell;
                        ClsConnection.Conn.Close();
                        //MessageBox.Show(cell.Value.ToString());
                        if (cell.Value != null)
                        {
                            MessageBox.Show(row.Cells[0].Value.ToString());
                        }
                      

                    }
//SELECT is the datagrid header name
   
v2
Comments
Scubapro 25-Jan-12 2:35am
   
Next time be specific in your question about GridView or DataGridView, as they are totally different objects!
SantoshRohinSantosh 25-Jan-12 4:23am
   
Okay

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