Runtime DataGrid formating

T

Tina

I have a datagrid where I specified {0:###,###,###} at design time. However
sometimes I am putting percentages in the grid instead of money and I want
to change the formating to {##%}. I've looked over the docs on such things
as the dataformatstring porperty of the boundColumn but still can't figure
out how to do it in code.

Thank,
T
 
J

Joseph Lee

This is what I did, it is not very good, but will allow you to do
processing.

Example:
On the aspx/ascx page, I would like to retrieve the the date for the the
particular record
so i call a function coded in the aspx.cs/ascx.cs which is GetDate, I will
pass in the record in the datagrid

<%# GetDate(Container.DataItem) %>

On the aspx.cs/ascx.cs, I will process the dataItem, which return the short
date for my datagrid

public string GetDate(Object obj)
{
string topicDate = ((DataRowView)obj)["topicDate"].ToString();
return Convert.ToDateTime(topicDate).ToShortDateString();
}

-----------------------------------------------------------------

If you would like a better and much more professional way, this article
might help

http://openmymind.net/databinding/index.html

Joey
 
T

Tina

Ouch, pretty ugly but thank you for the help. The article is also very
good.
T

Joseph Lee said:
This is what I did, it is not very good, but will allow you to do
processing.

Example:
On the aspx/ascx page, I would like to retrieve the the date for the the
particular record
so i call a function coded in the aspx.cs/ascx.cs which is GetDate, I will
pass in the record in the datagrid

<%# GetDate(Container.DataItem) %>

On the aspx.cs/ascx.cs, I will process the dataItem, which return the
short date for my datagrid

public string GetDate(Object obj)
{
string topicDate = ((DataRowView)obj)["topicDate"].ToString();
return Convert.ToDateTime(topicDate).ToShortDateString();
}

-----------------------------------------------------------------

If you would like a better and much more professional way, this article
might help

http://openmymind.net/databinding/index.html

Joey

Tina said:
I have a datagrid where I specified {0:###,###,###} at design time.
However sometimes I am putting percentages in the grid instead of money
and I want to change the formating to {##%}. I've looked over the docs on
such things as the dataformatstring porperty of the boundColumn but still
can't figure out how to do it in code.

Thank,
T
 
T

Tina

Joseph,
I found a way to do it without having to put code manually into the .aspx
file.
In the ItemDataBound event just do:
myString = e.item.Cells(x).text 'the number without editing mask
myInteger = val(myString)
e.item.cells(x).text = myInteger.tostring("##%")

Works great!
T


Joseph Lee said:
This is what I did, it is not very good, but will allow you to do
processing.

Example:
On the aspx/ascx page, I would like to retrieve the the date for the the
particular record
so i call a function coded in the aspx.cs/ascx.cs which is GetDate, I will
pass in the record in the datagrid

<%# GetDate(Container.DataItem) %>

On the aspx.cs/ascx.cs, I will process the dataItem, which return the
short date for my datagrid

public string GetDate(Object obj)
{
string topicDate = ((DataRowView)obj)["topicDate"].ToString();
return Convert.ToDateTime(topicDate).ToShortDateString();
}

-----------------------------------------------------------------

If you would like a better and much more professional way, this article
might help

http://openmymind.net/databinding/index.html

Joey

Tina said:
I have a datagrid where I specified {0:###,###,###} at design time.
However sometimes I am putting percentages in the grid instead of money
and I want to change the formating to {##%}. I've looked over the docs on
such things as the dataformatstring porperty of the boundColumn but still
can't figure out how to do it in code.

Thank,
T
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top