format?

  • Thread starter Thread starter LL
  • Start date Start date
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>
 
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
 
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?
 
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
 
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?
 
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
 
The sort function code here:
private void MyDataGrid_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)

{

BindGrid(e.SortExpression);

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