Note that there are some explanatory texts on larger screens.

plurals
  1. POFormat columns for ASP.Net GridView based on data type
    text
    copied!<p>I have a very simple ASP.Net page that acts as a front end for a stored procedure. It just runs the procedure and shows the output using a gridview control: less than 40 lines of total code, including aspx markup. The stored procedure itself is very... volatile. It's used for a number of purposes and the output format changes regularly. </p> <p>The whole thing works great, because the gridview control doesn't really need to care what columns the stored procedure returns: it just shows them on the page, which is exactly what I want.</p> <p>However, the database this runs against has a number of datetime columns all over the place where the time portion isn't really important- it's always zeroed out. What I would like to be able to do is control the formatting of just the datetime columns in the gridview, without ever knowing precisely which columns those will be. Any time a column in the results has a datetime type, just apply a given format string that will trim off the time component.</p> <p>I know I could convert to a varchar at the database, but I'd really don't want to have to make developers care about formatting in the query and this belongs at the presentation level anyway. Any other ideas?</p> <hr> <p>Finally got this working in an acceptable (or at least improved) way using this code:</p> <pre><code>Protected Sub OnRowDatabound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim d As DateTime For Each cell As TableCell In e.Row.Cells If Date.TryParse(cell.Text, d) AndAlso d.TimeOfDay.Ticks = 0 Then cell.Text = d.ToShortDateString() End If Next cell End If End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload