format?

Discussion in 'ASP .Net' started by LL, Nov 4, 2003.

  1. LL

    LL Guest

    Hi,

    How to format 3000.00 to
    $3000?

    Thanks.
    LL, Nov 4, 2003
    #1
    1. Advertising

  2. Hi

    > How to format 3000.00 to
    > $3000?


    double x = 3000.00;
    string z = string.Format("{0:C}", x);

    Ciao
    Giorgio
    Giorgio Parmeggiani, Nov 4, 2003
    #2
    1. Advertising

  3. LL

    LL Guest

    Thanks Giorgio,

    How to do this in a grid:

    <ItemTemplate>
    <asp:Label id=Label2 runat="server" Text='<%#
    Convert.ToDouble(DataBinder.Eval(Container.DataItem, "Price").ToString())
    %>'>
    </asp:Label>
    </ItemTemplate>


    "Giorgio Parmeggiani" <> wrote in message
    news:...
    > Hi
    >
    > > How to format 3000.00 to
    > > $3000?

    >
    > double x = 3000.00;
    > string z = string.Format("{0:C}", x);
    >
    > Ciao
    > Giorgio
    >
    >
    >
    LL, Nov 4, 2003
    #3
  4. LL

    LL Guest

    Thanks for the help.


    > double x = 3000.00;
    > string z = string.Format("{0:C}", x);


    This one will return $3,000,00. I need it to return $3,000 only. How to do
    that?



    "Giorgio Parmeggiani" <> wrote in message
    news:...
    > Hi
    >
    > > How to format 3000.00 to
    > > $3000?

    >
    >
    > Ciao
    > Giorgio
    >
    >
    >
    LL, Nov 4, 2003
    #4
  5. Hi

    > <ItemTemplate>
    > <asp:Label id=Label2 runat="server" Text='<%#
    > Convert.ToDouble(DataBinder.Eval(Container.DataItem, "Price").ToString())
    > %>'>
    > </asp:Label>
    > </ItemTemplate>


    You can write:

    <ItemTemplate>
    <asp:Label id=Label2 runat="server" Text='<%#
    Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    "Price").ToString("$#"))
    %>'>
    </asp:Label>
    </ItemTemplate>

    Ciao
    Giorgio
    Giorgio Parmeggiani, Nov 4, 2003
    #5
  6. LL

    LL Guest

    Thanks Giorgio.

    Got this error :
    Compiler Error Message: CS1501: No overload for method 'ToString' takes '1'
    arguments

    Source Error:

    Line 54: <asp:Label id=Label2 runat="server" Text='<%#
    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString("$#"))
    %>'>

    ToString() is fine. Any ideas?



    "Giorgio Parmeggiani" <> wrote in message
    news:...
    > Hi
    >
    > > <ItemTemplate>
    > > <asp:Label id=Label2 runat="server" Text='<%#
    > > Convert.ToDouble(DataBinder.Eval(Container.DataItem,

    "Price").ToString())
    > > %>'>
    > > </asp:Label>
    > > </ItemTemplate>

    >
    > You can write:
    >
    > <ItemTemplate>
    > <asp:Label id=Label2 runat="server" Text='<%#
    > Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    > "Price").ToString("$#"))
    > %>'>
    > </asp:Label>
    > </ItemTemplate>
    >
    > Ciao
    > Giorgio
    >
    >
    LL, Nov 4, 2003
    #6
  7. Hi

    > Line 54: <asp:Label id=Label2 runat="server" Text='<%#
    >

    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString("$#"))
    > %>'>
    >


    Try this:
    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString()).ToS
    tring("$#")

    Ciao
    Giorgio
    Giorgio Parmeggiani, Nov 4, 2003
    #7
  8. LL

    LL Guest

    Thanks Giorgio.

    It works now. But I got another problem.
    The "Sort" function work before(when click the header column), but now got
    this error:

    Input string was not in a correct format.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.FormatException: Input string was not in a correct
    format.

    Source Error:

    Line xx: <asp:Label id=Label2 runat="server" Text='<%#
    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString()).ToS
    tring("$#") %>'>

    Can I do this onDatabind event?



    "Giorgio Parmeggiani" <> wrote in message
    news:...
    > Hi
    >
    > > Line 54: <asp:Label id=Label2 runat="server" Text='<%#
    > >

    >

    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString("$#"))
    > > %>'>
    > >

    >
    > Try this:
    >

    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString()).ToS
    > tring("$#")
    >
    > Ciao
    > Giorgio
    >
    >
    LL, Nov 4, 2003
    #8
  9. Uhmm!

    > The "Sort" function work before(when click the header column), but now got
    > this error:
    >
    > Input string was not in a correct format.

    .... cut....
    >
    > Line xx: <asp:Label id=Label2 runat="server" Text='<%#
    >

    Convert.ToDouble(DataBinder.Eval(Container.DataItem,"Price").ToString()).ToS
    > tring("$#") %>'>


    Whats the code of your sort function?

    Giorgio
    Giorgio Parmeggiani, Nov 4, 2003
    #9
  10. LL

    LL Guest

    The sort function code here:
    private void MyDataGrid_SortCommand(object source,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

    {

    BindGrid(e.SortExpression);

    }
    LL, Nov 4, 2003
    #10
  11. double x = 3000.00;
    string z = x.ToString("c");

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Big Things are made up of
    Lots of Little Things.

    "Giorgio Parmeggiani" <> wrote in message
    news:...
    > Hi
    >
    > > How to format 3000.00 to
    > > $3000?

    >
    > double x = 3000.00;
    > string z = string.Format("{0:C}", x);
    >
    > Ciao
    > Giorgio
    >
    >
    >
    Kevin Spencer, Nov 4, 2003
    #11
  12. > BindGrid(e.SortExpression);

    What is BindGrid() ?

    Hi
    Giorgio Parmeggiani, Nov 5, 2003
    #12
  13. LL

    LL Guest

    Thanks for the help!

    Here is the BindGrid function:

    public void BindGrid(String sortfield)
    {
    string sqlStr = "SELECT * FROM X ";

    SqlDataAdapter myCommand = new SqlDataAdapter(sqlStr, myConnection);

    DataSet ds = new DataSet();
    myCommand.Fill(ds);


    DataView Source = ds.Tables[0].DefaultView;

    if (sortfield != "")
    Source.Sort = sortfield;


    MyDataGrid.DataSource=Source;
    MyDataGrid.DataBind();
    }
    "Giorgio Parmeggiani" <> wrote in message
    news:eOcFv$...
    > > BindGrid(e.SortExpression);

    >
    > What is BindGrid() ?
    >
    > Hi
    >
    >
    LL, Nov 5, 2003
    #13
  14. Hi

    I think that the problem is due to the fact that the some row of your
    database table have the field "Price" empty.

    Tries to write in the page Html
    <ItemTemplate>
    <asp:Label id = "Label2" runat = "server" Text = '<% #
    ConvertToDouble(DataBinder.Eval(Container.DataItem, "Price"))%> '>
    </ asp:Label>
    </ ItemTemplate>

    And in the codebehind define the function:

    Protected Function ConvertToDouble(ByVal value As Object) As String
    If value Is System.DBNull.Value Then
    ConvertToDouble = String.Empty
    Else
    ConvertToDouble = Convert.ToDouble(value). ToString ("$#")
    End If
    End Function

    Ciao
    Giorgio
    Giorgio Parmeggiani, Nov 5, 2003
    #14
  15. LL

    LL Guest

    "Giorgio Parmeggiani" <> wrote in message
    news:...
    > Hi
    >
    > I think that the problem is due to the fact that the some row of your
    > database table have the field "Price" empty.


    That's it!

    Thanks.
    LL, Nov 5, 2003
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,130
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,225
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,570
  4. shannon
    Replies:
    0
    Views:
    551
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    447
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page