467,161 Members | 1,031 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

Date format in datagrid

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
Apr 6 '06 #1
  • viewed: 4920
Share:
2 Replies
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.

Apr 6 '06 #2
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" <De**********@discussions.microsoft.com> wrote in message
news:2B**********************************@microsof t.com...
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

Apr 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mike L | last post: by
2 posts views Thread by Roger | last post: by
2 posts views Thread by Wayne Wengert | last post: by
4 posts views Thread by Paul | last post: by
5 posts views Thread by William Gower | last post: by
4 posts views Thread by yer darn tootin | last post: by
1 post views Thread by Niclas Lindblom | last post: by
1 post views Thread by Lurc | last post: by
6 posts views Thread by yoshitha | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.