Solved

Set myDataColumn.DataType in Gridview

Posted on 2010-11-22
5
675 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net File format issue in producing Excel file 3 37
Hey!!!! 1 22
asp.net mvc5 6 17
insert value of checklistbox checked 4 27
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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