Click here to Skip to main content
15,904,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have taken checkbox within GridView and succeeded to display the checked value but i want to add that numbers.
I can't understand where will I put addition coding and what is that?

StringBuilder str = new StringBuilder();
       // Select the checkboxes from the GridView control
       for (int i = 0; i < GridView2.Rows.Count; i++)
       {
           GridViewRow row = GridView2.Rows[i];
           bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
           if (isChecked)
           {
               // Column 1 is the "Rate" column of price
               str.Append(GridView2.Rows[i].Cells[1].Text);
           }
       }
       // prints out the result
       Response.Write(str.ToString());
Posted
Updated 28-Mar-11 21:31pm
v3
Comments
m@dhu 29-Mar-11 3:26am    
Always use pre tags for code for better readability.

1 solution

It isn't too clear from your question what problem you are having - I assume that English is not your native language? So, I will have to make some assumptions:
1) You have a GridView, which displays two or more columns.
2) One of these is a Checkbox, called "CheckBox1", and the user (or your code) checks some CheckBoxes, and not others.
3) If a CheckBox is checked, include the value of the Rate column (Column 1, or the second column in the GridView) in a running total.

If this is correct, then:

// Select the checkboxes from the GridView control
int totalRate = 0;
for (int i = 0; i < GridView2.Rows.Count; i++)
{
    GridViewRow row = GridView2.Rows[i];
    bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
    if (isChecked)
    {
        // Column 1 is the "Rate" column of price
        totalRate += int.Parse(GridView2.Rows[i].Cells[1].Text);
    }
}
// prints out the result
Response.Write(totalRate.ToString());
You may need to change the int to another data type - if so do not forget to change in in both places!

If this is not correct, then what are you trying to do?
 
Share this answer
 
Comments
manasBonton 29-Mar-11 3:52am    
i have taken the Rate in Money data type.so it is displaying error"Input string was not in a correct format.
"
manasBonton 29-Mar-11 3:54am    
I got the result thanking you i have taken double
OriginalGriff 29-Mar-11 4:01am    
I was just about to ask what text was in the cell!

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