c# - Varchar[HH:MM] SUM in DataGridView -
i have datagridview cells contains varchar values hh:mm format, when need sum of these values utilize function
private void calctime { string span = datagridview1.columns["horas"].tostring(); double seconds = 0; seconds = datagridview1.rows .cast<datagridviewrow>() .asenumerable() .sum(x => timespan.parse((x.cells["horas"].value.tostring())).totalseconds); string somat = ""; double segundosc = seconds; somat = string.format( "{0:00}:{1:00}", segundosc / 3600, (segundosc / 60) % 60, segundosc % 60); }
if values 01:00 or 03:00 it's right if had values 01:30 sum doesn't work. below:
how can create work right? regards
the problem sum of seconds 20400 , when split 3600 you'll 5.666666. , because variable double
, using format {0:00}
, value rounded. either need cast sum int
or utilize math.floor
.
int seconds = (int)datagridview1.rows .cast<datagridviewrow>() .asenumerable() .sum(x => timespan.parse((x.cells["horas"].value.tostring())).totalseconds);
or
somat = string.format( "{0:00}:{1:00}", math.floor(segundosc / 3600), (segundosc / 60) % 60);
also don't need lastly parameter in string.format
.
another alternative convert timespan
timespan.fromseconds
.
double seconds = datagridview1.rows .cast<datagridviewrow>() .asenumerable() .sum(x => timespan.parse((x.cells["horas"].value.tostring())).totalseconds); timespan totaltime = timespan.fromseconds(seconds); string somat = totaltime.tostring(@"hh\:mm");
c# winforms datagridview
No comments:
Post a Comment