How do I set the datagrid column to currency format?

G

Guest

Data in datagrid shows all numbers to the 1000th place. So, $3.00 is shown
3.0000

How do I set the datagrid column to currency format?

DataGridColumnStyle colStyle4 = new
DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle4);
dgts.GridColumnStyles[4].Width = 75;
colStyle4.MappingName = dt.Columns[4].ColumnName;
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Thanks for your post.

We can set DataGridTextBoxColumn.Format to "c" to get the currency format.
For more information, please refer to DataGridTextBoxColumn.Format in MSDN
documentation.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

The example shows connecting to a table, but I'm connecting to a SP.

I get the error, "Object reference not set to an instance of an object."

DataGridColumnStyle colStyle4 = new
DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle4);
dgts.GridColumnStyles[4].Width = 75;
DataGridTextBoxColumn myGridTextBoxColumn =
(DataGridTextBoxColumn)dgPrivileges.TableStyles["dbo.SALES_REVENUE"].GridColumnStyles["GROSS_FEE_AMOUNT"];
myGridTextBoxColumn.Format = "c";
colStyle4.MappingName = dt.Columns[4].ColumnName;

I have also tried using dt.TableName but recieved error, "Cannot convert
type 'string' to 'System.Windows.Forms.DataGridTextBoxColumn'

DataGridTextBoxColumn myGridTextBoxColumn =
(DataGridTextBoxColumn)dt.TableName;
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Thanks for your feedback.

I think you may have some misunderstanding of winform databinding. DataGrid
will have no relation to do with database, it only can be bound to .Net
data object, such as DataTable, DataSet, ArrayList etc.... So it does not
care whether the data comes from SP or a SQL statement.

Normally, at DataGrid formatting side, you should first correctly get the
DataGridTextBoxColumn reference. The exception you got is
NullReferenceException, so I think myGridTextBoxColumn you got must be a
Null reference, which caused the problem, so please check this to ensure
that it gets the correct DataGridTextBoxColumn valid reference.

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top