![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello All: I have a GridView in a user control that has its datasource assigned dynamically. It's working well, however, date values are displaying mm/dd/yyyy hh:mm:ss and I only want a short date format. I don't know which column will hold a date prior to runtime. Is there any way to set it on the fly? -Bob |
#3
| |||
| |||
|
|
Hi Bob, Interesting challenge. It took a while but I came up with something that *seems* to work. See the comments inline in the code sample Could you give the code below a test and see if it does what you want? Let us know? Ken Microsoft MVP [ASP.NET] %@ control language="VB" classname="dgridusr" % script runat="server" Protected Sub Page_Load _ (ByVal sender As Object, ByVal e As System.EventArgs) If Not IsPostBack Then GridView1.DataSource = CreateDataSource() GridView1.DataBind() End If End Sub Protected Sub GridView1_RowDataBound _ (ByVal sender As Object, ByVal e As _ System.Web.UI.WebControls.GridViewRowEventArgs) ' Routine to display an unknown DateTime column ' as a short date at runtime ' By Ken Cox [Microsoft MVP] ' July 28/06 ' ' Make sure this is a datarow being bound If e.Row.RowType = DataControlRowType.DataRow Then ' Create variables to hold values Dim dtview As System.Data.DataRowView Dim dt As DateTime Dim intCounter As Integer ' Get the contents of the current row ' as a DataRowView dtview = e.Row.DataItem ' Loop through the individual values in the ' DataRowView's ItemArray For intCounter = 0 To _ dtview.Row.ItemArray.Length - 1 ' Check if the current value is ' a System.DateTime type If TypeOf dtview.Row.Item(intCounter) Is _ System.DateTime Then ' If it is a DateTime, cast it as such ' into the variable dt = dtview.Row.Item(intCounter) ' Set the text of the current cell ' as the short date representation ' of the datetime e.Row.Cells(intCounter).Text = _ dt.ToShortDateString End If Next End If End Sub Function CreateDataSource() As Data.DataTable ' Provides some dummy data Dim dt As New Data.DataTable Dim dr As Data.DataRow dt.Columns.Add(New Data.DataColumn _ ("IntegerValue", GetType(Int32))) dt.Columns.Add(New Data.DataColumn _ ("StringValue", GetType(String))) dt.Columns.Add(New Data.DataColumn _ ("CurrencyValue", GetType(Double))) dt.Columns.Add(New Data.DataColumn _ ("DateTime", GetType(DateTime))) Dim i As Integer For i = 0 To 5 dr = dt.NewRow() dr(0) = i dr(1) = "Item " + i.ToString() dr(2) = 1.23 * (i + 1) dr(3) = Now.AddDays(i) dt.Rows.Add(dr) Next i Return dt End Function /script asp:gridview id="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound" /asp:gridview "Bob Phillips" <phil1630 (AT) bellsouth (DOT) net> wrote in message news:1154030179.908776.70110 (AT) i3g2000cwc (DOT) googlegroups.com... Hello All: I have a GridView in a user control that has its datasource assigned dynamically. It's working well, however, date values are displaying mm/dd/yyyy hh:mm:ss and I only want a short date format. I don't know which column will hold a date prior to runtime. Is there any way to set it on the fly? -Bob |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |