How can I format numbers or strings in a DataRepeater?

Technology: .NET, SQL Server 2008 R2, Winforms

Well, for my life I cannot understand this.

First of all, I use a DataTable to store data that comes from a SQL Server 2008 database, and I bind it to a DataRepeater.

I tried to change the binding as follows:

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

which works great with text fields and labels elsewhere but not on DataRepeater. (label1 is part of the ItemTemplate related to DataRepeater)

Since data binding like this does not work, I want to just take my DataTable and just make the column have the format specified above.

And manually changing the data format: (it's floating)

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

Doesn't work, the datarepeater just changes it.

I want it:

 12,123,123.00 

and I get the following:

 12123123 

Any ideas?

+4
source share
3 answers

I think this is your DataTable story that converts values ​​back to double . When the data type of the column is double (which I suspect), it takes a string representation of double and kindly converts it back.

You must add the computed column to the DataTable and populate it with a string representation of the numeric value.

By the way: you forgot i++ in your for statement.

+1
source

Sorry for my bad english. This works great for me.

 private void textBox10_TextChanged(object sender, EventArgs e) { string f = String.Format("{0:#0.00}", Convert.ToDouble(((TextBox)sender).Text)); ((TextBox)sender).Text = f; } 

Example:

textBox10.Text= 48
result= 48.00

Change this code for different data types.

This uses the TextChanged text field event in the Datarepeater .

+2
source

if you want to automatically convert 12123123 to 12,123,123; put this code in the _TextChanged event of your text field:

 int i = ((TextBox)sender).SelectionStart; if (((TextBox)sender).Text != "") { string f = String.Format("{0:N0}", Convert.ToDouble(((TextBox)sender).Text)); ((TextBox)sender).Text = f; int len; len = ((TextBox)sender).Text.Replace(",", "").Length; if ((len %= 3) == 1) { i += 1; } ((TextBox)sender).SelectionStart = i; } 
0
source

All Articles