Click here to Skip to main content
11,795,848 members (76,065 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Windows WinForm
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 at 28-Mar-13 9:08am
Why are you reposting the exact same question again?
shacha at 28-Mar-13 9:10am
No i dint i just updated the question.
ProgramFOX at 28-Mar-13 9:12am
You can update a question by clicking on the "Improve question" button.
shacha at 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 at 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 at 28-Mar-13 13:09pm
In which event we have to write this??

What is indexOfCheckBoxColumn??
Maciej Los at 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 at 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 at 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 at 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 at 28-Mar-13 15:22pm
corrected ;)
shacha at 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
0 Maciej Los 350
1 Sergey Alexandrovich Kryukov 326
2 KrunalRohit 239
3 Andy Lanng 236
4 OriginalGriff 230
0 OriginalGriff 1,852
1 Maciej Los 1,465
2 KrunalRohit 1,300
3 CPallini 840
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2015
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