Formatting fields in a datatable

  • Thread starter Thread starter Rotsey
  • Start date Start date
R

Rotsey

Hi,

I have written a custom multi column listbox control that I pass a
datatable to.

the issue is that if there are date fields then these may
be in the wrong format or any other field formatting

So when I create the control I pass a object that defines
the label, width and format of each column.

How do I use the format field (which is a string) to generically
format a any field the in the datatable.

mystring = dr.ToString(myheader.Format];

Something like that is what I want to do.

But ToString doesn't support format

any ideas
rotsey.
 
Well, you could check if dr supports IFormattable; this has a
ToString(string format, IFormatProvider formatProvider)

Marc
 
Sorry dr is a DatatRow object
precisely: hence, dr is the value of the i'th column, i.e. the
value you want to format... so if your column is a date, then this is
a boxed DateTime - which *does* implement IFormattable:

object val = dr;
IFormattable fVal = val as IFormattable;
string sVal = fVal == null ? Convert.ToString(val)
: fVal.ToString(format, CultureInfo.CurrentCulture);

Marc
 
ok thanks

Marc Gravell said:
Sorry dr is a DatatRow object
precisely: hence, dr is the value of the i'th column, i.e. the value
you want to format... so if your column is a date, then this is a boxed
DateTime - which *does* implement IFormattable:

object val = dr;
IFormattable fVal = val as IFormattable;
string sVal = fVal == null ? Convert.ToString(val)
: fVal.ToString(format, CultureInfo.CurrentCulture);

Marc
 

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

Back
Top