Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i Have Two DataGridView in WinForm, in WinForm My Data Transfer From DataGridView1 To DataGridView2 By Check Box.
i Have Problem in Calculation "RemaningDays"

This Data Show in DataGridView2, Calculation Not Work Properly,in DataGridView2 only First Row Calculate Correctly and Remain Row Only Repeat First Row Calculation

Result Show
LastDay	     --------            Today----	          RemaningDays
16/5/2020	-----        4/5/2020  ----       	12
16/6/2020	     -----   4/5/2020	----            12
16/7/2020	      -----  4/5/2020	----            12

if i Remove First Row Then Result Show
LastDay	    --------     Today	  ----  RemaningDays
16/6/2020	-----4/5/2020	----  43
16/7/2020	-----4/5/2020	----  43

Please Help me Solve This Problem

What I have tried:

My Code

C#
foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                DateTime dt3 = Convert.ToDateTime(dataGridView2.CurrentRow.Cells["Today"].Value);
                DateTime dt1 = Convert.ToDateTime(dataGridView2.CurrentRow.Cells["LastDate"].Value);
                int s12 = (dt1 - dt3).Days;
                
                
                row.Cells["RemaningDays"].Value = s12;

                dataGridView2.Columns["LastDate"].ReadOnly = true;
                dataGridView2.Columns["Today"].ReadOnly = true;
		dataGridView2.Columns["RemaningDays"].ReadOnly = true;
                
            }
Posted
Updated 3-May-20 21:09pm
v4

You are not using the row variable:
C#
foreach (DataGridViewRow row in dataGridView2.Rows)
{            
   DateTime dt3 = DateTime.Parse(row.Cells["Today"].Value);
   DateTime dt1 = DateTime.Parse(row.Cells["LastDate"].Value);
   int s12 = (dt1 - dt3).Days;
                
   row.Cells["RemaningDays"].Value = s12;

   // ...
}
 
Share this answer
 
Comments
Amar chand123 4-May-20 3:12am    
Thank You Sir
phil.o 4-May-20 3:28am    
You're welcome.
Instead of dataGridView2.CurrentRow.Cells use: row.Cells
 
Share this answer
 
Comments
Amar chand123 4-May-20 3:13am    
Thank You Sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900