Transform data in GridView control

Discussion in 'ASP .Net Web Controls' started by VancouverMike, Mar 14, 2007.

  1. 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
    VancouverMike, Mar 14, 2007
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], Mar 14, 2007
    #2
    1. Advertising

  3. Thanks Walter, this is exactly what I want.

    Mike
    VancouverMike, Mar 14, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manuel
    Replies:
    1
    Views:
    398
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Oct 13, 2005
  2. Matt
    Replies:
    1
    Views:
    1,263
    Martin Honnen
    Apr 22, 2004
  3. Replies:
    0
    Views:
    501
  4. Replies:
    1
    Views:
    672
    Joris Gillis
    Feb 2, 2005
  5. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,957
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
Loading...

Share This Page