4

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?

Christian
  • 503
  • 3
  • 7
  • 21

3 Answers3

2

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.

Marko
  • 20,385
  • 13
  • 48
  • 64
1

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.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • I'll consider this a possible solution, but it's not best when you have large amounts of data that require to be formatted. Which I have to take into account. Let's wait and see if there are other solutions out there. Before I close this. Also, it's actually a float, and I erased the Convert.To statement as it's redundant in this case, but was used in a previous part of the application and copy pasted. – Christian Oct 05 '11 at 22:02
  • Making this the answer as this was the only solution that worked with a datarepeater – Christian Nov 16 '11 at 19:24
0

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;
        }
sjd_zare
  • 1
  • 2