Question

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?

Was it helpful?

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.

OTHER TIPS

Sorry for my bad english. This works fine 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 each other data types.

That uses TextChanged event of the textbox in to Datarepeater.

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

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;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top