By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,318 Members | 2,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,318 IT Pros & Developers. It's quick & easy.

Date format in datagrid

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.