Transform data in GridView control

V

VancouverMike

Hi,

I am using Visula Studio 2005, C# and ASP.NET 2.0 with SQL Server 2005. I am
using GridView control(databound to dataset) to display data from database. I
have a smalldatetime type of field in database, and I would like to display
this field in two columns in GridView control, one for Month and one for
Year. For the Month column in GridView control, I would like to display the
full name of the month, i.e. for Month 1 display January, for Month 2 display
February, etc. I can use DatePart function in Stored Proc to get numeric
month and year from the smalldatetime field, but I don't know how to
transform the numeric month to a full name month in GridView control. I would
just like to show the data, in read mode, not in Edit mode.

Could anybody give me a hand on this?

Thanks a lot,
Mike
 
W

Walter Wang [MSFT]

Hi Mike,

Based on my understanding, you have a stored procedure is returning a
result set contains two fields which are computed from a smalldatetime
field, one for Month, one for Year, both are in integer data type. For the
Month field, you'd like to display January for 1, February for 2, etc. in a
GridView. Please correct me if I've misunderstood anything.

To do this, you will have to create a TemplateField and handle GridView's
RowDataBound event to manually convert the month number into a string,
using this way, you also don't have to use DatePart in stored procedure to
return the month and year separately, you can directly get a DateTime
object at server-side code and use it accordingly:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="lastUpdated"
HeaderText="lastUpdated" SortExpression="lastUpdated" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblMonth" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView data =
(System.Data.DataRowView)e.Row.DataItem;
DateTime lastUpdated = (DateTime) data["lastUpdated"];
Label lblMonth = (Label)e.Row.Cells[2].FindControl("lblMonth");
lblMonth.Text = string.Format("{0:MMMM}", lastUpdated);
}
}


Hope this helps.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top