Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have checkboxes inside datagridview and when the user selects some checkboxes and click the button the price should be calculated how can i achieve this?
Posted
Comments
[no name] 28-Mar-13 9:08am    
Why are you reposting the exact same question again?
shacha 28-Mar-13 9:10am    
No i dint i just updated the question.
Thomas Daniels 28-Mar-13 9:12am    
You can update a question by clicking on the "Improve question" button.
shacha 28-Mar-13 9:13am    
That is what i did i dint reposted the question may be i am having browsers issues refreshed the window 2 times.

DataGridView contains DataGridViewRows collection. Each row in the collection is DataGridViewRow[^].

To get the total sum of selected rows, you need to iterate through the collection of rows:
C#
    private void button1_Click(object sender, EventArgs e)
    {
        DataGridViewCell dc = null;
        DataGridViewCheckBoxCell cbx = null;
        double mySum = 0;
        string s = string.Empty;
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            dc = dr.Cells[2];
            cbx = (DataGridViewCheckBoxCell)dc;
            if (Convert.ToBoolean(cbx.Value))
            {
                mySum += double.Parse(dr.Cells[0].Value.ToString());
            }
        }
        s = String.Format("Total sum: {0}", mySum.ToString());
        MessageBox.Show(s);
    }
}
 
Share this answer
 
v3
Comments
shacha 28-Mar-13 13:09pm    
In which event we have to write this??

What is indexOfCheckBoxColumn??
Maciej Los 28-Mar-13 13:14pm    
Oh, Gosh... are you a total beginner?
You can use Button_Click event. How to do it? Add a button on a form and double click on it to call Button_Click event.

IndexOfXXXXX - is a number of column in which checkbox/price is stored.
shacha 28-Mar-13 13:17pm    
ohh thats ok i know it...but i have price column as 2nd column in datagridview.I am some what newbie to winforms....Will this work for multiple checkbox selected??
Maciej Los 28-Mar-13 13:26pm    
Yes, it could works for multiple selected checkboxes inside DataGridView. Just replace IndexOfXXXX with correct column number. That's all.
shacha 28-Mar-13 13:37pm    
Cannot convert type 'System.Windows.Forms.DataGridViewCell' to 'System.Windows.Forms.CheckBox'

I am getting this error at CheckBox c = (CheckBox)row.Cells[2];
Checkbox have a property IsChecked property. U can use it to calculate your pricing values.
For those check boxes which are check u can perform price calculation for them only. U can use this property to differentiate between check boxes which are checked and which are not.
 
Share this answer
 
Comments
shacha 28-Mar-13 9:35am    
How can i find checkbox inside datagridview??

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