Click here to Skip to main content
13,201,452 members (70,213 online)
Rate this:
Please Sign up or sign in to vote.
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 28-Mar-13 3:06am
ThePhantomUpvoter 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.
ProgramFOX 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
shacha 28-Mar-13 9:35am
How can i find checkbox inside datagridview??
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
    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());
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];
Maciej Los 28-Mar-13 15:22pm
corrected ;)
shacha 28-Mar-13 23:28pm
Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewCheckBoxCell'.

I am getting this error at cbx = (DataGridViewCheckBoxCell)dc;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100