Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi I am trying to read from an excel file to my asp.net pages. Everything displays fines except the column the has decimal values in it. In my excel files I am restricting the Cell format to only show 2 decimal but some how when I read this in to my asp page it display all the decimals... I want to truncte the values to 2 decimals only can some one guide me how to do this...the code I using to display an excel file in asp is as follows:
private void Page_Load(object sender, System.EventArgs e)
{
DataSet myDataSet = new DataSet();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\exceltest.xls;" + "Extended Properties=\"Excel 8.0;\"";
OleDbDataAdapter myData = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
myData.TableMappings.Add("Table", "ExcelTest");
myData.Fill(myDataSet);
dgrdSample.DataSource = myDataSet.Tables[0].DefaultView;
dgrdSample.DataBind();
}how can I trucate the output to 2 decimals...I am using C# to code... Will appreciate any help...Thanks

I can think of a few ways, but probably the best is using the DataBinder.eval method.
Format Data and Handle Null Values in a DataGrid Web Control
Other options would be to loop through the dataset and round each value yourself, put in another column in the data table with an expression to round the source column's values, or round at item binding by handling the DataGrid.OnItemDataBound event.
-SN

Sorry about the double posting... I used the Gaurish's method and it work fine
(("SELECT ROUND (Column1,2) FROM [Sheet1$]", strConn);
but now the only problem I have is... after rounding the values in this column the trailing zeros get cut off(1.50 changes to 1.5)...I need to keep the trailing zeros as the represent price...if can help me on how I can still display the trailing zeros that would be a lot of help...thanks

![]() |
Browsing CVS Repositories
|
How to check external URL...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |