12,998,394 members (72,442 online)
Rate this:
See more:
when i run as follows in datagridview in windows applciation

``` Course    GS        VB    SJ     RK      MS(faculty)      Total

REO                                                        10
AFF                                                        10```

total number of faculty is 5
total hours of course is 10

when i divide the total(10) by number of faculty (5) and give the answer 2.

i want to display the 2 for each faculty GS,VB,SJ,RK,Ms.

i want the output as follows;

``` Course    GS        VB    SJ     RK      MS(faculty)      Total

REO      2         2     2       2      2                 10
AFF      2         2     2       2      2                 10```

when click the Load button 2 has to displayed in datagridview for each faculty.

for that i have using this formula, total (10) is divide by number of faculty(5) gives the answer 2

this 2 will display in datagridview for each faculty.

for th above output how can i do using csharp.

Note: it is a windows application

Posted 16-Feb-13 22:04pm
Updated 16-Feb-13 22:18pm
v2
OriginalGriff 17-Feb-13 4:19am

Where is your data coming from?
How do you get it into the DataGridView at the moment

Rate this:

## Solution 1

Logic can be as follows...

1. On Load button click, Loop through all the rows of DataGridView using `foreach` statement like below...

For Each row, inside the loop do the following.

2. Find the Total column value for the row.

3. Apply logic "Total/No._Of_Faculties" and find the result.

4. Find all the Columns related to Faculties and assign this value to them.

For looping code will look like below.
```// Write this inside the load button click event.
foreach(DataGridViewRow row in dgvCourses.Rows)
{
// Do task for each row using "row".
}```

Update
Code may be like below.
```// Write this inside the load button click event.
int totalHours = 0;
int facultyHour = 0;

foreach(DataGridViewRow row in dgvCourses.Rows)
{
// Do task for each row using "row".
totalHours = Convert.ToInt32(row.Cells["total"].Value); // Here "total" is the column name.

facultyHour = totalHours /  5 ;

// Assign this value to the below cells.
//GS        VB    SJ     RK      MS
row.Cells["GS"].Value = facultyHour.ToString();
row.Cells["VB"].Value = facultyHour.ToString();
row.Cells["SJ"].Value = facultyHour.ToString();
row.Cells["RK"].Value = facultyHour.ToString();
row.Cells["MS"].Value = facultyHour.ToString();
}```

NOTE
Replace the Cell names in the code correctly.
I have not tested this code. So, just debug and see what is happening. But follow this logic.
v2
Member 8054539 17-Feb-13 20:31pm

you said the above answer ok.

but i am new to one applying loop for each.

kindly please tell me how can i do?

please send the code and help me.

Top Experts
Last 24hrsThis month
 ppolymorphe 275 OriginalGriff 200 RickZeeland 190 F-ES Sitecore 133 Peter Leow 110
 OriginalGriff 4,497 ppolymorphe 2,277 Kornfeld Eliyahu Peter 1,973 RickZeeland 1,951 Richard Deeming 1,593