Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Datatable in which values are like 0.98,0.97,1.00,0.85478

These values are binded to a dropdownlist .
I want the values in dropdownlist to be like 98,97,100,85.Can any1 help me to achieve it.?
I had used DataFormatTextString.But its not working


/binding ddl in pageload
C#
public void bindddlscore()
       {
           DataTable dtscore = (DataTable)Session["GrdSearch"];
           DataView dvscore = dtscore.DefaultView;
           DataTable distinctscore = dvscore.ToTable(true, "Score");
           ddlscore.DataSource = distinctscore;
           ddlscore.DataTextField = "Score";
           ddlscore.DataTextFormatString = "{0:0%}";
           ddlscore.DataBind();
           ddlscore.Items.Insert(0, "All");
       }
Posted
Updated 21-Jul-15 2:44am
v2

ddlscore.DataTextFormatString ="{0:N}";
 
Share this answer
 
Comments
Member 11833198 22-Jul-15 5:47am    
Thanks for ur answer..I tried but not working ..:-( any other suggestion?
Do you mean like another culture than en-US?

if so you can use an IFormatProvider, such as CultureInfo, in such case you could simply change the cultureinfo of the thread you're operating on.
For instance say da-DK!
This culture have , as digit seperator and ; as list seperator and . as thousands seperator instead of the en-US that use . for digits and , for lists.

in this case you'd be able to use
DataTable dtscore = (DataTable)Session["GrdSearch"];
DataView dvscore = dtscore.DefaultView;
DataTable distinctscore = dvscore.ToTable(true, "Score");
distinctscore.Locale = new CultureInfo("da-DK");
ddlscore.DataSource = distinctscore;
ddlscore.DataTextField = "Score";
ddlscore.DataTextFormatString = "{0:0.0%}";
ddlscore.DataBind();


I'm updating solution with a somewhat more explicit implementation to examplify and also updating where the Locale goes as i'd put it on the control not the table by mistake B-]. My page has a single dropdown control with the name DropDown.

using System;
using System.Data;
using System.Globalization;

public partial class DataTableBinding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dataTable = GetTable();            
            DataView dataView = dataTable.DefaultView;
            DataTable dataTableDistinct = dataView.ToTable(true, "Score");
            dataTableDistinct.Locale = new CultureInfo("da-DK");
            DropDown.DataSource = dataTableDistinct;
            DropDown.DataTextField = "Score";
            DropDown.DataTextFormatString =  "{0:0.0%}";
            DropDown.DataBind();
            DropDown.Items.Insert(0, "ALL");
        }
    }

    private static DataTable GetTable()
    {
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("Score", typeof(decimal));
        DataRow row = dataTable.NewRow();
        row["Score"] = 0.98;
        dataTable.Rows.Add(row);

        row = dataTable.NewRow();
        row["Score"] = 0.97;
        dataTable.Rows.Add(row);

        row = dataTable.NewRow();
        row["Score"] = 1.00;
        dataTable.Rows.Add(row);

        row = dataTable.NewRow();
        row["Score"] = 0.85478;
        dataTable.Rows.Add(row);

        return dataTable;
    }
 
Share this answer
 
v3
Comments
Member 11833198 22-Jul-15 5:59am    
Thanks for it.But I cant understand what is that locale?
Thomas Nielsen - getCore 22-Jul-15 7:03am    
So you have data and you want to push it to percentage, like multiplying with 100 and your % in DataTextFormatString takes care of that, the Locale handles your other request, to change the decimal point from . to , ( see https://msdn.microsoft.com/en-us/library/system.data.datatable.locale(v=vs.110).aspx )
Member 11833198 22-Jul-15 8:45am    
Thanks alot for it.But Datatextformatting is not working in my case :-(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900