Member Avatar for cool_intentions

Hello people,
I`m learning C# end need some help with my assignment. I have Form with DataGridView on it end one Button. When I click Button, I should get SUM from only selected cells into Message Box. How can I do that?
Thanks for your help.

Member Avatar for cool_intentions

Thanks, I found the answer. But how can I enable that user can select only Unit Price columns?
My answer:

private void button1_Click(object sender, EventArgs e)
        {
            
            double i = 0;
            foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
            {
                i = i + double.Parse(cell.Value.ToString());
            
            }
            MessageBox.Show(i.ToString());
        }

Made mistake here, time I go to sleep I guess...

After a good nights sleep I found this: http://gissolved.blogspot.com/2009/08/datagridview-tricks.html
It says something about CellStateChanged event.
Adapted it to exclude 1 column (column 2)
Tested it and it works.Edit: if you set the readonly of the other cells to false!

private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            //exclude the cells of column 2 from being set to false
            if (e.StateChanged == DataGridViewElementStates.Selected &&
                e.Cell.ColumnIndex != 1)
                e.Cell.Selected = false;
        }

Success!

Member Avatar for cool_intentions

Thanks a lot ddanbe. I finally did it. Hire is my completed assignment. Thanks for your help. :D

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Assignment_1___Cene_proizvoda
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'northwindDataSet.Products' table. You can move, or remove it, as needed.
            this.productsTableAdapter.Fill(this.northwindDataSet.Products);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double i = 0;
            foreach (DataGridViewCell celija in dataGridView1.SelectedCells)
            {
                i = i + double.Parse(celija.Value.ToString());
            
            }
            MessageBox.Show("Rezultat: " + i.ToString());
        }

        private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            if (e.StateChanged == DataGridViewElementStates.Selected && e.Cell.ColumnIndex != 5)
                e.Cell.Selected = false;
        }
  
    }
}

Always glad if I can help sort it out :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.