views:

202

answers:

6

I have a Gridview that has a timestamp as one of the rows. When I read the data from the database the data is in the format( mm/dd/yyyy hh:mm:ss ). I've figured out how to format the way I want it which is just the ( mm/dd/yyyy ) and droping of the (hh:mm:ss) with the following code:

  Dim numrows2 = GridView1.Rows.Count
  For i = 0 To numrows2 - 1
        Dim acc = Left(GridView1.Rows(i).Cells(0).Text, 10)
        GridView1.Rows(i).Cells(0).Text = acc
  Next i

The problem is that this gridview has like 5 pages and this will only work for the current page. For instance,

  1. initial load of the page the 1st page will be formatted correctly
  2. I click page 3, this code will format page 1, so page 3 won't be formatted
  3. I click page 1, the code will format page 3

So it's basically formatting the current page, but not the selected page.

I either need to be able to format every row of the gridview everytime, or be able to figure out the page selected and format that page. I don't know how to do either.

Any help would be appreciated.

A: 

You can format the column for the date needed at design time or at runtime with some properties and it will do it for all rows.

Here's a tutorial on it. Has a nice table of the formats you can use.... http://shawpnendu.blogspot.com/2009/04/how-to-format-gridview-rowscolumns-in.html

klabranche
+1  A: 

do you use boundfield?

there is a property called DataFormatString, to get the format you want set this to "MM/dd/yyyy" then you dont need to do any formatting at all in the code behind

Pharabus
the link klablanche attached shows this much better than my example
Pharabus
+2  A: 

Use formatting in the datacolumn like so:

<asp:BoundField HeaderText="Date" DataField="SomeDate" DataFormatString="{0:MM/dd/yyyy}">
rdkleine
A: 

What you want to do is to use the RowDataBound event and do your databinding at that point. The ToString method of the DateTime object can be overridden based on the format code you need. Doing it this way you do it like this:

   Private Sub FormatMyGridView _
               (ByVal sender As Object, _
                ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
           Handles gvMyGridView.RowDataBound

      Dim drItems As DataRow

      ' Grid Column Layout (Handy for formatting multiple items)
      ' 0 - View Details Hyperlink
      ' 1 - Id
      ' 2 - Department #
      ' 3 - Request Type
      ' 4 - Employee Number
      ' 5 - Employee Name
      ' 6 - Status
      ' 7 - Effective Date

      If e.Row.RowType = DataControlRowType.DataRow Then

         drRequest =DirectCast(e.Row.DataItem, System.Data.DataRowView).Row

         e.Row.Cells(7).Text = drItems.MyDateField.ToString("MM/dd/yyyy")

      End If    

   End Sub
Dillie-O
A: 

The best way to do this would probably be by using an ObjectDataSource and binding to that, you could format the date data there and return some business object. ObjectDataSource also allows for more efficient paging than a simple SqlDataSource (I'm assuming that's what you're using?).

If you are just running a dynamic SQL query, you can format the returned data there (something like):

SELECT CONVERT(VARCHAR, CONVERT(DATETIME, OriginalDateValue, 101), 101)

Does that help? If you need specific code for an ObjectDataSource just say so.

MKing
+1  A: 

Use DataFormatString, but don't forget to add HtmlEncode="false" :

<asp :BoundField DataField="DateColumn"
 DataFormatString="{0:MM/dd/yyyy}"
 HtmlEncode="false" />
Canavar