Hi there

I have a small problem. I have a datagridview on my winform. I also have a label that will display hours worked. What I would like to do is when I select a whole row in the datagridview, I want the label mentioned to display hours worked for the selected row. My first column in the datagridview is my date column. When I select a whole row, the label must show how many hours are worked. There are also a column called office hours where the hours are stored.

It gets a bit tricky, because there are rows with the same date. So in this case it must count all hours in rows with the same date, and display the total hours in the label.

I would've added some code, but I don't know what code to show you, so please any help? Ask me if I must add some code.

Maybe you can do the following on the form designer:
1)Set the datagrid properties in order to select all the row at opnce, and to multi row select = false in order to have only one selected row.
2)Add the event handler for the SelectedIndexChanged event in order to be able to calculate the total hours worked.

On the source for the SelectedIndexChanged event handler you can
1) Get the selected row into selectedRow
2) get the date from this row into selectedDate
3) define an integer totalHours with a value of 0
3) for each currentRow in the datagrid.Rows
3.1) if the date of the currentRow is equals to the selectedDate then
3.1.1) add the officeHours value to the totalHours
4) lets your label equals to totalHours to string

Hope this helps

:) this might just work...don't you perhaps have some sample code on the steps mentioned here:

On the source for the SelectedIndexChanged event handler you can
1) Get the selected row into selectedRow
2) get the date from this row into selectedDate
3) define an integer totalHours with a value of 0
3) for each currentRow in the datagrid.Rows
3.1) if the date of the currentRow is equals to the selectedDate then
3.1.1) add the officeHours value to the totalHours
4) lets your label equals to totalHours to string

Okay so this is what I have so far..

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            workedHoursLbl.Text = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
        }

This just sets the workedHoursLbl's text to the select rows hours column. Now I need to add a for each statement and some source. Can you perhaps help with this?

Try som thing like

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    string dateWorked = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[dateWorkedCell].Value.ToString();
    int workedHours = 0;
    foreach( var curRow in dataGridView1.rows) 
    {
        if (curRow.Cells[dateWorkedCell].Value.TosString()==dateWorked)
            workedHours += (int) curRow.Cells[dateWorkedCell].Value;
    }
    workedHoursLbl.Text = workedHours.Tostring();
}

This code has not been tested.

Hope this helps

Thanks for the help..

I just seem to get an error between the brackets of my foreach statement. It says 'The object does not contain a definition for cells....(Missing assembly reference)

This is my code after you gave it to me, I modified it a bit:

//workedHoursLbl.Text = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
            string dateWorked = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells[6].Value.ToString();
            int workedHours = 0;
            foreach (var curRow in dataGridView1.Rows)
            {
                if (curRow.Cells[6].Value.TosString() == dateWorked)
                    workedHours += (int)curRow.Cells[6].Value;
            }
            workedHoursLbl.Text = workedHours.ToString();

Sorry, you need to force the type of curRow:

foreach (DataGridViewRow curRow in dataGridView1.Rows)

(VS2010 / .NET 4.0 Client)

Hope this helps

Edited 5 Years Ago by lolafuertes: n/a

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