TimeSpan.ToSting is broken

M

Marcel Müller

Example:

new TimeSpan(725651).ToString(@"s\.fFF")
results in
"0.007"
which is off by one decade.

new TimeSpan(725651).ToString(@"s\.ffF")
results in
"0.07"
which is not entirely wrong, but still not correct. The expected result is
"0.072"
in both cases.

(Framework 4.5)


Marcel
 
A

Arne Vajhøj

Example:

new TimeSpan(725651).ToString(@"s\.fFF")
results in
"0.007"
which is off by one decade.

new TimeSpan(725651).ToString(@"s\.ffF")
results in
"0.07"
which is not entirely wrong, but still not correct. The expected result is
"0.072"
in both cases.

I must admit that I can not see the problem.

For 0.072 then I read the docs as:

s => "0"
f => "0"
F => ""
ff => "07"
FF => "07"
fff = "072"
FFF => "072"

so:

s.fFF => "0.007"
s.ffF => "0.070"

which is exactly what you see.

Arne
 
M

Marcel Müller

I must admit that I can not see the problem. [...]
s.fFF => "0.007"
s.ffF => "0.070"

which is exactly what you see.

Seems that I expected the possibility to mix f and F in a way so that I
can set a minimum of required fractional digits, similar to 0 and # in
number formatting. I did not come into my mind that it happens to give
exactly this result if the fields are treated independently. (I also
found no option to drop the decimal dot in case of no fractionals, but
this is another question.)


Marcel
 
A

Arne Vajhøj

I must admit that I can not see the problem. [...]
s.fFF => "0.007"
s.ffF => "0.070"

which is exactly what you see.

Seems that I expected the possibility to mix f and F in a way so that I
can set a minimum of required fractional digits, similar to 0 and # in
number formatting.

Maybe .TotalSeconds.ToString("0.00##") would work for you.

Arne
 
M

Marcel Müller

Maybe .TotalSeconds.ToString("0.00##") would work for you.

Yes, but unfortunately it is not that easy to implement this
transformation to a bound column of a DataGrid or RadGrid. So I reverted
to "s\.FFF".


Marcel
 
A

Arne Vajhøj

Yes, but unfortunately it is not that easy to implement this
transformation to a bound column of a DataGrid or RadGrid. So I reverted
to "s\.FFF".

If you used DataGridView instead of DataGrid then you could use
a cell formatter like:

public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
List<Data> lst = new List<Data>();
lst.Add(new Data { ID = 1, Delta = new TimeSpan(100000) });
lst.Add(new Data { ID = 2, Delta = new TimeSpan(110000) });
lst.Add(new Data { ID = 3, Delta = new TimeSpan(120000) });
dataGridView1.CellFormatting += FixTimeSpan;
dataGridView1.DataSource = lst;
}
private static void FixTimeSpan(object sender,
DataGridViewCellFormattingEventArgs e)
{
if(e.Value.GetType() == typeof(TimeSpan))
{
e.Value =
((TimeSpan)e.Value).TotalSeconds.ToString("0.00##");
e.FormattingApplied = true;
}
}
}
public class Data
{
public int ID { get; set; }
public TimeSpan Delta { get; set; }
}

Arne
 
M

Marcel Müller

If you used DataGridView instead of DataGrid then you could use
a cell formatter like:

Oh sorry, I never used DataGrid since .NET 2.0. I think it is called
GridView, isn't it? But in this particular case it is a Telerik RadGrid.
I don't know whether they have a similar formatting hook. In fact I
expected a column specific hook, rather than a grid specific one.
dataGridView1.CellFormatting += FixTimeSpan;

I will check this.


Marcel
 
B

bradbury9

El domingo, 29 de diciembre de 2013 09:03:00 UTC+1, Marcel Müller escribió:
Oh sorry, I never used DataGrid since .NET 2.0. I think it is called

GridView, isn't it? But in this particular case it is a Telerik RadGrid.

I don't know whether they have a similar formatting hook. In fact I

expected a column specific hook, rather than a grid specific one.






I will check this.





Marcel

I must say it or I will explode... Telerik's ASP.NET RadGrid sucks. It is quite troublesome if you try to mix designer created columns and runtime added columns. It is true that provides some cool filtering capabilities,

That being said, RadGrid allows a couple of ways of tweaking the columns itprovides at runtime. Maybe there are more choices, I know only two:

a) Create a new class and inherit from its columns.
b) Use its columns but use a custom template with the desired formatting logic. You should create a class that derived from ITemplate.

The most simple choice could be b), but cannot do any quick test/sample because I am on holiday and don't have access to my devel machine.

Hope it help
 
M

Marcel Müller

I must say it or I will explode... Telerik's ASP.NET RadGrid sucks.

I know, and on IE <10 incredibly slow.
It is quite troublesome if you try to mix designer created columns and runtime added columns.

But no one uses the designer here.

In fact we derived from the RadGrid and added a second data source
(XDataSource) for the Columns. So you can bind Columns and their
sequence as well as the data rows.

However, we fixed the issue you mentioned on the way. If the XDataSource
contains a simple string with "@UniqueNameOfTheColumn", you can insert
columns defined in aspx wherever you like. If it does not contain this
string, the column is invisible.
It is true that provides some cool filtering capabilities,

That being said, RadGrid allows a couple of ways of tweaking the columns it provides at runtime. Maybe there are more choices, I know only two:

a) Create a new class and inherit from its columns.
b) Use its columns but use a custom template with the desired formatting logic. You should create a class that derived from ITemplate.

The most simple choice could be b), but cannot do any quick test/sample because I am on holiday and don't have access to my devel machine.

Our extension already provides a column factory, so b) is straight
forward. But normally it only creates BoundGridColumns (not sure with
the name).

In fact I have already implemented b) for type TimeSpan in the code for
older Framework 3.5 where TimeSpan.ToString() did nothing useful, except
for debugging. But once you go this way, you loose all the cool
features. Even sorting does no longer work. And the Telerik Column
classes are implemented in a way that you can neither reasonably extend
them nor add a new one.


Marcel
 
B

bradbury9

El lunes, 30 de diciembre de 2013 07:03:10 UTC+1, Marcel Müller escribió:
My work partners are used to designer, had to instruct them to do it dinamically.

I decided to keep a more simple approach, designed something similar to this (two layers, no business layer):

Note Entity class has properties to make easier working with grids.

public abstract class GridContainer<T> : Control where T: Entity
{

protected abstract RadGrid Grid { get; }
protected abstract List<T> GetGridData();
protected abstract void GenerateColumns();

Grid_OnInit()
{
GenerateColumns();
}

Grid_OnDataNeeded()
{
this.Grid.DataSource = this.GetGridData();
}

[methods to add dinamically columns to this.Grid]

[manage all usual RadGrid events (OnCommand, etc)]

}

then in the ascx.cs:

protected partial class CustomerGrid : GridContainer<Customer>
{

protected RadGrid Grid { get {return this.rgCustomers; } }
protected List<Customer> GetGridData()
{
DalCustomer dal = new DalCustomer();
return dal.Get();
}

protected void GenerateColumns()
{
AddBoundColumn(headerText: "Customer name", dataField: "Name", allowFilter:true, allowSort:true);
AddCommandColumn(headerText: "Send email", commandName: "email", SendEmail);
}

private void SendEmail(Customer commandEntity)

}

Your solution is pretty smart, but hard to implement. Once done it is quitestraigh to work with. I Choose just to work to to dinamically generated columns and work with lists of objects as datasource.

Sad but true
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top