I have this small monitoring system which monitors customers on daily, weekly, and monthly payments.

How do i sum all the numeric values in the "type of payment column"?
How do i delete the current record selected by the user? IE, i select a name, then hit delete, deletes all the record from that current name Selected..

Thanks

Edited 5 Years Ago by RudolfRyan: n/a

Attachments ask.jpg 56.03 KB

I have this small monitoring system which monitors customers on daily, weekly, and monthly payments.

How do i sum all the numeric values in the "type of payment column"?
How do i delete the current record selected by the user? IE, i select a name, then hit delete, deletes all the record from that current name Selected..

Thanks

I manage to solve the addition thing,
the deleting of record base on the user selected cell is the one i am trying to solve.

Is your DGV bind to dataSource?
If so you can do a filter with "SUM Columns", which will ack like an sql query.

If you dont use any dataSource, you can loop through rows and sum cell in this partiucular column, like:

decimal total = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                total += Convert.ToDecimal(row.Cells[1].ToString());
            }
            MessageBox.Show("Total is: " + total.ToString());

To delate the currently selected cell (I think like you want to delete a whole row, not only a selected number), you can do:

foreach (DataGridViewRow row in dataGridView1.SelectedCells)
            {
                if (row.Index > -1)
                {
                    dataGridView1.Rows.RemoveAt(row.Index);
                }
            }

Is your DGV bind to dataSource?
If so you can do a filter with "SUM Columns", which will ack like an sql query.

If you dont use any dataSource, you can loop through rows and sum cell in this partiucular column, like:

decimal total = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                total += Convert.ToDecimal(row.Cells[1].ToString());
            }
            MessageBox.Show("Total is: " + total.ToString());

To delate the currently selected cell (I think like you want to delete a whole row, not only a selected number), you can do:

foreach (DataGridViewRow row in dataGridView1.SelectedCells)
            {
                if (row.Index > -1)
                {
                    dataGridView1.Rows.RemoveAt(row.Index);
                }
            }

My GridView is connected to an SQL database with a datasource. I manage to sum all the value.

I tried the code for the deletion but it is giving me an error

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewRow'.

If your DGV is connected to a dataSouse, you have to delete value in dataSource, not in dgv.
So get the index of the selected row in dgv, and then delete the row in dataTable, based on the row index.

So do:

int rowIndex = 0;
            foreach (DataGridViewRow row in dataGridView1.SelectedCells)
            {
                rowIndex= row.Index;
                break;
            }
            table.Rows.RemoveAt(rowIndex);

sorry, you have to loop through DGVTextBoxCell (this is the default one):

private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewTextBoxCell cell in dataGridView1.SelectedCells)
            {
                if (cell.ColumnIndex > -1)
                {
                    table.Rows.RemoveAt(cell.ColumnIndex);
                }
            }
        }

sorry, you have to loop through DGVTextBoxCell (this is the default one):

private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewTextBoxCell cell in dataGridView1.SelectedCells)
            {
                if (cell.ColumnIndex > -1)
                {
                    table.Rows.RemoveAt(cell.ColumnIndex);
                }
            }
        }

Ok thanks for all the help, what i did was add a column with a customer iD,

int i = view.CurrentRow.Index;
selected = Convert.ToInt32(view.Rows.Cells["Customer#"].Value.ToString());
string sqldel = string.Format("delete from [TodayTable] where Customer# {0}", selected);


i will try the codes you gave me. thanks again

This article has been dead for over six months. Start a new discussion instead.