Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello can anyone help me here
i got this code

DateTime d1;
            DateTime d2;
            for(int i=0;i<dataGridView1.RowCount-1;i++)
            {
                d1 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value);
                d2 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[1].Value);
                TimeSpan ts = d1 - d2;
                dataGridView1.Rows[i].Cells[3].Value = ts.Days;
            }

but it seems that i'm doing it wrong
the error says 'String was not recognized as a valid DateTime'.
and i also had this code to be appeared in my datagridview

dataGridView1.ColumnCount = 4;
            dataGridView1.Columns[0].Name = "Book";
            dataGridView1.Columns[1].Name = "Borrowed date";
            dataGridView1.Columns[2].Name = "Delivered date";
            dataGridView1.Columns[3].Name = "Time Span";

            dataGridView1.Rows.Add("Book 1", "13.01.2018", "20.01.2018");
            dataGridView1.Rows.Add("Book 2", "05.03.2018", "13.03.2018");
            dataGridView1.Rows.Add("Book 3", "20.04.2018", "15.05.2018");


What I have tried:

i tried to look in stack overflow but i just get the same converting method
Posted
Updated 4-Jul-19 9:15am

C#
string format = "dd.MM.yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;

string s1 = "13.01.2018";

DateTime dtm = DateTime.ParseExact(s1, format, provider);
I would encourage you to intercept errors using this:
string format = "dd.MM.yyyy";
CultureInfo provider = CultureInfo.InvariantCulture;

string s1 = "13.01.2018";

DateTime auDate;

if (DateTime.TryParseExact(s1, format, provider, DateTimeStyles.None, out auDate))
{
    // 'auDate has valid DateTime
}
else
{
    throw new InvalidDataException("error parsing Datetime");
}
 
Share this answer
 
Comments
Maciej Los 4-Jul-19 3:37am    
5ed!
BillWoodruff 4-Jul-19 9:07am    
thanks, Maciej !
Alternativelly to solution #1 by BillWoodruff[^], you can use DateTime.ParseExact Method (System) | Microsoft Docs[^] to convert string into proper DateTime.

C#
string sdate = "13.08.2018";	
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("DE-de");
string format = "dd.MM.yyyy";

DateTime d = DateTime.ParseExact(sdate, format, ci);
Console.WriteLine($"{d}");


Here is the way to convert string-dates to proper dates via Linq query:

C#
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("DE-de");
string format = "dd.MM.yyyy";
dt = dataGridView1.Rows.Cast<DataGridViewRow>()
    .Where(x =>x.Cells[0].Value!=null)
    .Select(x => dt.LoadDataRow(new object[]
            {
                x.Cells[0].Value,
                DateTime.ParseExact(x.Cells[1].Value.ToString(), format, ci),
                DateTime.ParseExact(x.Cells[2].Value.ToString(), format, ci),
                (DateTime.ParseExact(x.Cells[2].Value.ToString(), format, ci) - DateTime.ParseExact(x.Cells[1].Value.ToString(), format, ci)).Days
            }, false))
    .CopyToDataTable();

dataGridView1.Columns.Clear();
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = dt;
 
Share this answer
 
v2

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