Search code examples
c#datatabledatarepeater

How can I format numbers or strings in a DataRepeater?


Tech: .NET, SQL Server 2008 R2, Winforms

Ok, for the life of me, I cannot figure this out.

First and foremost, I'm using a DataTable to store the data, which is coming from an SQL server 2008 database, and I'm binding it to a DataRepeater.

I've tried changing the binding like this:

label1.DataBindings.Add("Text", history, "Value", true, DataSourceUpdateMode.Never, "", "N");

which works great on textboxes and labels elsewhere, but not on the DataRepeater. (label1 is part of the ItemTemplate associated with the DataRepeater)

Since binding the data like that isn't working, I want to just take my DataTable and just force the column to have the format listed above.

And manually changing the format of the data: (it's a Float)

for (int i=0;i < history.Rows.Count;i++)
{
    history.Rows[i]["Value"] = String.Format("{0:N}", history.Rows[i]["Value"]);
}

Doesn't work either, the datarepeater just changes it back.

I want this:

12,123,123.00

and I get this:

12123123

Any ideas?


Solution

  • I think it is your DataTable "history" that converts the values back to double. When the column's data type is double (which I suspect) then it accepts a string representation of a double and kindly converts it back.

    You should add a computed column to your DataTable and fill it with the string representation of the numeric value.

    BTW: you forgot a i++ in your for statement.