c# - Regarding GridViews and DateTime columns -


the goal of question me have better understanding of going on. seems there different ways solve issue depending on how data coming in.

the situation when pull in data stored procedure lot of times there datetime columns , @ point have display them on web page. now, depending on how data coming in, seems have use different solution. depends on if i'm displaying inside label or if i'm displaying in gridview. understand going on , best way handle is.

so here situations i've run , did solve issue (after researching on site of course).

situation 1:
datetime column stored procedure being saved datatable , used datasource gridview autogeneratecolumns set false.

my solution situation format date on ascx page

<asp:templatefield>     <itemtemplate>         <asp:label id="lbltest1" runat="server" text='<%# eval("somedatefield", "{0:mm/dd/yyyy}") %>'></asp:label>     </itemtemplate> </asp:templatefield> 

situation 2: same above, except autogeneratecolumns set true

my solution take care of formatting on rowdatabound

protected void gvappraisal_rowdatabound(object sender, gridviewroweventargs e) {     if (e.row.rowtype == datacontrolrowtype.datarow)     {         foreach (datacontrolfieldcell cell in e.row.cells)         {             autogeneratedfield field = cell.containingfield autogeneratedfield;             if (field != null && field.datatype == typeof(datetime))                 cell.text = datetime.parse(cell.text).toshortdatestring();             }         }     } } 

situation 3: datacolumn stored procedure being saved datatable using value inside label somewhere on page.

i'm thinking above method work, change data shortdate, i'll have testing on later see. made helper extension method handle such:

public static string parseformatteddatefromstring(this string datestring, string dateformat = "mm/dd/yyyy") {     string formatteddate = "";     datetime date;     if(datetime.tryparse(datestring, out date))     {         formatteddate = date.tostring(dateformat);     }     return formatteddate; } 

my third example seems work long i'm not dealing gridview, when use gridview solution won't work. solution works 3 scenarios.

now, trial , error, issue gridview forcing particular date/time format when datasource set? can't wrap head around why seems work differently when you're dealing autogeneratedcolumns vs non autogeneratedcolumns.

i looked solutions casting columns date, though data looked fine on sql server, still bringing on time segment when ran program.

thanks ahead of time understanding issue, in end i'm trying grow programmer , knowing going on in background rather using solution got work without knowing causing work.

edit jaydipj's solution down in comments work situation i'm making new stored procedure. however, of time have use pre-existing stored procedure won't able make changes to. --joseph


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -