format?

L

LL

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>
 
L

LL

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?
 
G

Giorgio Parmeggiani

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
 
L

LL

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?
 
G

Giorgio Parmeggiani

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
 
L

LL

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?
 
G

Giorgio Parmeggiani

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
 
L

LL

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

{

BindGrid(e.SortExpression);

}
 
L

LL

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();
}
 
G

Giorgio Parmeggiani

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
 
L

LL

Giorgio Parmeggiani said:
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.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top