Solved

Set myDataColumn.DataType in Gridview

Posted on 2010-11-22
5
670 Views
Last Modified: 2012-06-22
I was just wondeirng if anyone could explain how to change the  DataType associated with a gridview in C#?  The gridview is bound to a sqldatasource, however, one field that I want to format (Amount) as a currency is being automatically set as an integer column.  So when I try to reformat the mydatarowview['Amount'] in the Rowcreated event I get a failure when I go to write the updated value as its now a string.

If there is a better way to go about this I would love to hear it as I am just coming over from years of PHP so ASP is still fairly new to me.  I know how to go about this with SQL if need be but I really wanted to get it sorted out on my ASP page.

TIA
0
Comment
Question by:mgabr123
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34191330
can you post what you are doing at the moment that is causing the problem?
0
 

Author Comment

by:mgabr123
ID: 34191416
Double testdouble = Convert.ToDouble(myDataRowView["Amount"].ToString());

 String teststring = String.Format("{0:C}", testdouble);

 myDataRowView["Amount"] = teststring;

Open in new window


I basically am running into problems on the third line.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34191434
Ah ok. If you are doing that then you can simply parse the value back to double to store:

myDataRowView["Amount"] = double.Parse(teststring);

Open in new window

0
 

Author Comment

by:mgabr123
ID: 34191500
Even with that added it still threw an error:

"Input string was not in a correct format"
Double testdouble = Convert.ToDouble(myDataRowView["Amount"].ToString());

                    String teststring = String.Format("{0:C}", testdouble);

                    myDataRowView["Amount"] = double.Parse(teststring);

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34191654
Ah, sorry, wasn't paying attention :)

Try:
myDateRowView["Amount"] = double.Parse(teststring, System.Globalization.NumberStyles.AllowCurrencySymbol);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now