-1

hi every one!
I have a datagridview which contain 5 column (data source is a table in ms acces). i want to add 3rd and 4th column using loop
but there is an error occured "input string was not in correct format"

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)    //loop to add the total and from column
{string StartTime = dataGridView1.Rows[i].Cells[3].Value.ToString();
 string Total = dataGridView1.Rows[i].Cells[2].Value.ToString();
TimeSpan spantotal = TimeSpan.Parse(Total); //convert f into timespan and store in a
 TimeSpan spanStartTime = TimeSpan.Parse(StartTime);
TimeSpan SpanEndTime = (spantotal.Add(spanStartTime));// sum of timespan a and b store in c
 TimeEnd = new DateTime(SpanEndTime.Ticks);
 string strTimeEnd = TimeEnd.ToString("hh:mm tt");
dataGridView1.Rows[i].Cells[4].Value = strTimeEnd;   //assign c to to.value
dataGridView1.Rows[i + 1].Cells[3].Value = dataGridView1.Rows[i].Cells[4].Value;//assign to.cell0.value to from.cell1.value
Votes + Comments
Same question twice?
3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by ejazmusavi
0

The error is in this line

TimeSpan spantotal = TimeSpan.Parse(Total);
0

Well the error says clearly that the variable Total is not in the correct format. This could only mean that dataGridView1.Rows.Cells[2].Value does not contain something that can be parsed to a TimeSpan. Have you done a debug already?

0

dataGridView1.Rows.Cells[2].Value already contain a value
(11:10) but when later i see this value using messageBox it look like (06/10/2010 11:10:00 AM)
while i have set the default cell format usin this code

dataGridView1.Columns[2].DefaultCellStyle.Format = "hh:mm";
0

>DefaultCellStyle.Format = "hh:mm";
if you had done
DefaultCellStyle.Format = "d"; you would have got 6. Underneath there is always the full blown DateTime structure.
But have you already set a breakpoint and run your program so it would stop there so you could see what all the values of the variables are?

0

DataGrid cell return vaalue of DateTime datatype.

You may use

... 
 DateTime date = DateTime.Parse(total);
 TimeSpan ts = date.TimeOfDay;
 ..

or

TimeSpan ts=((DateTime)dataGridView1.Rows[i].Cells[2].Value).TimeOfDay;
0

Thank You Sir!
This solved my problem. But now i want to extract the day from the time which is the result of addition of two time.

TimeSpan spantotaltime = ((DateTime)dataGridView1.Rows[i].Cells[2].Value).TimeOfDay;
TimeSpan spanFromTime = ((DateTime)dataGridView1.Rows[i].Cells[3].Value).TimeOfDay;
 TimeSpan spanToTime = spantotaltime.Add(spanFromTime);

I want to get the day of spanToTimewhich must vary when i add the nest timespan and so on..
Once again thank You So Much

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.