Date format in datagrid

Discussion in 'ASP .Net' started by =?Utf-8?B?RGVyZWsgVmluY2VudA==?=, Apr 6, 2006.

  1. What must I do to overcome a problem with my dates becoming formatted as
    "2/22/2525 12:00:00 AM" in the datagrid? I want to handle all dates as
    short string of format "2/22/2525." Otherwise when I run the update routine
    SQL (set start_date = "2/22/2525 12:00:00 AM") does not work while short
    string (set date = '2/22/2525') does work. See update routine below:

    Private Sub MarketingDataGrid_UpdateCommand(ByVal source As Object, ByVal e
    As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    MarketingDataGrid.UpdateCommand

    Dim ErrorMessage As String = Session("ErrorMessage")
    Dim ad_nameText As TextBox = CType(e.Item.Cells(3).Controls(0),
    TextBox)
    Dim advertisement_name As String = ad_nameText.Text
    Dim start_dateText As TextBox = CType(e.Item.Cells(4).Controls(0),
    TextBox)
    Dim start_date As String = start_dateText.Text
    Dim expiration_dateText As TextBox =
    CType(e.Item.Cells(5).Controls(0), TextBox)
    Dim expiration_date As String = expiration_dateText.Text
    Dim ad_typeText As TextBox = CType(e.Item.Cells(6).Controls(0),
    TextBox)
    Dim ad_type As String = ad_typeText.Text
    Dim advertiserText As TextBox = CType(e.Item.Cells(7).Controls(0),
    TextBox)
    Dim advertiser As String = advertiserText.Text
    Dim advertisement_descriptionText As TextBox =
    CType(e.Item.Cells(8).Controls(0), TextBox)
    Dim advertisement_description As String =
    advertisement_descriptionText.Text
    Dim source_id As String = Session("source_id")
    Try
    baseconnection.Close()
    BICDataAdapter.UpdateCommand = ucommand
    BICDataAdapter.UpdateCommand.Connection = baseconnection
    baseconnection.ConnectionString = ConnectString
    BICDataAdapter.UpdateCommand.CommandText = "Update Marketing set
    advertisement_name = '" & advertisement_name & _
    "',start_date = '" & start_date & "',expiration_date = '" &
    expiration_date & "',ad_type = '" & ad_type & _
    "',advertiser = '" & advertiser & "',advertisement_description =
    '" & advertisement_description & _
    "' where source_id = " & source_id
    baseconnection.Open()
    BICDataAdapter.Fill(BICDataSet, "marketing")
    MarketingDataGrid.DataSource = BICDataSet.Tables("MARKETING")
    MarketingDataGrid.DataBind()

    --
    Derek V. Taylor
    UCI Brain Imaging Center
    =?Utf-8?B?RGVyZWsgVmluY2VudA==?=, Apr 6, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?RGVyZWsgVmluY2VudA==?=

    sirfunusa Guest

    Well of course you could bind with a SELECT statement instead of
    directly to the table, and format the dates in your SELECT statement.
    Or, handle the data appropriately on the grid.DataItemBound event,
    which gives your row and cell level access as the grid is being
    populated.
    sirfunusa, Apr 6, 2006
    #2
    1. Advertising

  3. Hi Derek,

    You probably want to do your formatting in the datagrid in a bound column
    using dataformatstring.

    That way, you keep the data and its presentation as separate issues. Here's
    the idea:

    <asp:datagrid id="MarketingDataGrid" runat="server">
    <columns>
    <asp:boundcolumn datafield="start_date"
    dataformatstring="{0:d/M/yyyy}" headertext="Date and Time">
    </asp:boundcolumn>
    </columns>
    </asp:datagrid>

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]

    "Derek Vincent" <> wrote in message
    news:...
    > What must I do to overcome a problem with my dates becoming formatted as
    > "2/22/2525 12:00:00 AM" in the datagrid? I want to handle all dates as
    > short string of format "2/22/2525." Otherwise when I run the update
    > routine
    > SQL (set start_date = "2/22/2525 12:00:00 AM") does not work while short
    > string (set date = '2/22/2525') does work. See update routine below:
    >
    > Private Sub MarketingDataGrid_UpdateCommand(ByVal source As Object, ByVal
    > e
    > As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
    > MarketingDataGrid.UpdateCommand
    >
    > Dim ErrorMessage As String = Session("ErrorMessage")
    > Dim ad_nameText As TextBox = CType(e.Item.Cells(3).Controls(0),
    > TextBox)
    > Dim advertisement_name As String = ad_nameText.Text
    > Dim start_dateText As TextBox = CType(e.Item.Cells(4).Controls(0),
    > TextBox)
    > Dim start_date As String = start_dateText.Text
    > Dim expiration_dateText As TextBox =
    > CType(e.Item.Cells(5).Controls(0), TextBox)
    > Dim expiration_date As String = expiration_dateText.Text
    > Dim ad_typeText As TextBox = CType(e.Item.Cells(6).Controls(0),
    > TextBox)
    > Dim ad_type As String = ad_typeText.Text
    > Dim advertiserText As TextBox = CType(e.Item.Cells(7).Controls(0),
    > TextBox)
    > Dim advertiser As String = advertiserText.Text
    > Dim advertisement_descriptionText As TextBox =
    > CType(e.Item.Cells(8).Controls(0), TextBox)
    > Dim advertisement_description As String =
    > advertisement_descriptionText.Text
    > Dim source_id As String = Session("source_id")
    > Try
    > baseconnection.Close()
    > BICDataAdapter.UpdateCommand = ucommand
    > BICDataAdapter.UpdateCommand.Connection = baseconnection
    > baseconnection.ConnectionString = ConnectString
    > BICDataAdapter.UpdateCommand.CommandText = "Update Marketing
    > set
    > advertisement_name = '" & advertisement_name & _
    > "',start_date = '" & start_date & "',expiration_date = '" &
    > expiration_date & "',ad_type = '" & ad_type & _
    > "',advertiser = '" & advertiser & "',advertisement_description
    > =
    > '" & advertisement_description & _
    > "' where source_id = " & source_id
    > baseconnection.Open()
    > BICDataAdapter.Fill(BICDataSet, "marketing")
    > MarketingDataGrid.DataSource = BICDataSet.Tables("MARKETING")
    > MarketingDataGrid.DataBind()
    >
    > --
    > Derek V. Taylor
    > UCI Brain Imaging Center
    Ken Cox - Microsoft MVP, Apr 6, 2006
    #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. Brian Candy
    Replies:
    2
    Views:
    1,131
    Janaka
    Feb 18, 2004
  2. DC
    Replies:
    5
    Views:
    2,985
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,186
    Michael Borgwardt
    May 30, 2004
  4. Chu
    Replies:
    3
    Views:
    4,158
    Shawn
    Aug 15, 2006
  5. Josh Sharpe
    Replies:
    1
    Views:
    189
    Brian Candler
    Sep 21, 2010
Loading...

Share This Page