Date format in datagrid

G

Guest

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()
 
S

sirfunusa

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.
 
K

Ken Cox - Microsoft MVP

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]
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top