Formatting fields in a datatable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rotsey

    #1

    Formatting fields in a datatable

    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[i].ToString(myhea der.Format];

    Something like that is what I want to do.

    But ToString doesn't support format

    any ideas
    rotsey.


  • Marc Gravell

    #2
    Re: Formatting fields in a datatable

    Well, you could check if dr[i] supports IFormattable; this has a
    ToString(string format, IFormatProvider formatProvider)

    Marc


    Comment

    • Rotsey

      #3
      Re: Formatting fields in a datatable

      Sorry dr is a DatatRow object

      I don't think it does.

      Any other ideas?

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:OTBp4QW6HH A.3528@TK2MSFTN GP04.phx.gbl...
      Well, you could check if dr[i] supports IFormattable; this has a
      ToString(string format, IFormatProvider formatProvider)
      >
      Marc
      >

      Comment

      • Marc Gravell

        #4
        Re: Formatting fields in a datatable

        Sorry dr is a DatatRow object
        precisely: hence, dr[i] 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[i];
        IFormattable fVal = val as IFormattable;
        string sVal = fVal == null ? Convert.ToStrin g(val)
        : fVal.ToString(f ormat, CultureInfo.Cur rentCulture);

        Marc


        Comment

        • Rotsey

          #5
          Re: Formatting fields in a datatable

          ok thanks

          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:OjdaJpW6HH A.536@TK2MSFTNG P06.phx.gbl...
          >Sorry dr is a DatatRow object
          precisely: hence, dr[i] 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[i];
          IFormattable fVal = val as IFormattable;
          string sVal = fVal == null ? Convert.ToStrin g(val)
          : fVal.ToString(f ormat, CultureInfo.Cur rentCulture);
          >
          Marc
          >

          Comment

          Working...