c# - Date Format to a GridView populated with SQL in ASP.net - NOT boundfield -
this code creates gridview else happening c#, mean populated c# code:
<%@ page language="c#" autoeventwireup="true" codebehind="standby_journals email.aspx.cs" inherits="standby_journals_email.standby_journals_email" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:button id="button1" runat="server" text="sql" onclick="button1_click" /> <asp:button id="button2" runat="server" text="send" onclick="button2_click" /> <br /> <asp:gridview id="gridview1" runat="server" font-size="smaller"> </asp:gridview> </div> </form> </body> </html>
i need add date format gridview, if try use boundfield
, does, creates column @ beginning of table, this:
<asp:gridview id="gridview1" runat="server" font-size="smaller"> <columns> <asp:boundfield datafield="journal date" dataformatstring="{0:mmmm d, yyyy}" /> </columns> </asp:gridview>
this code:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.net; using system.net.mail; using system.data; using system.data.sqlclient; using system.io; namespace standby_journals_email { public partial class standby_journals_email : system.web.ui.page { sqlconnection vid = new sqlconnection("data source=xxxxxxxx; initial catalog=xxxxxxxxxxx; user id=xxxxxx; password=xxxxxxx"); protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { string sqlquery1 = "select * dbo.standby_journals_dqms [ledger grp] in ('actuals','grp_fxrate','consegrp') , [source] = 'con' or [source] 'q%' order [source], [systemdatetime],[systemtime] asc"; string str = sqlquery1; sqlcommand xp = new sqlcommand(str, vid); vid.open(); xp.executenonquery(); sqldataadapter da = new sqldataadapter(); da.selectcommand = xp; dataset ds = new dataset(); da.fill(ds, "name"); gridview1.datasource = ds; gridview1.databind(); vid.close(); } public override void verifyrenderinginserverform(control control) { /* verifies control rendered */ } protected void button2_click(object sender, eventargs e) { using (stringwriter sw = new stringwriter()) { using (htmltextwriter hw = new htmltextwriter(sw)) { string emailfrom = "xxxxxx@xxxxx.co.nz"; string emailto = "xxxxxx@xxxxx.co.nz"; string emailsubject = "this subject"; //string emailbody = "cuerpo del mensaje"; gridview1.rendercontrol(hw); stringreader sr = new stringreader(sw.tostring()); mailmessage mm = new mailmessage(emailfrom, emailto); mm.subject = emailsubject; mm.body = "report:<hr />" + sw.tostring(); ; mm.isbodyhtml = true; smtpclient smtp = new smtpclient(); smtp.host = "xxxxxxxxxxxxxx"; smtp.deliverymethod = system.net.mail.smtpdeliverymethod.network; smtp.port = 25; smtp.timeout = 20000; smtp.send(mm); } } } } }
anyone can in how affect columns in table created gridview?
set autogeneratecolumns
property of gridview false
(i think solution looking for) , add boundfields required columns.
or if using templatefields there options set date format templatefield
. no need add boundfield
like:
<asp:templatefield itemstyle-width = "100px" headertext = "date" sortexpression="date" > <itemtemplate> <asp:label id="lblreceived_at" runat="server" text='<%# eval("date","{0:d}")%>'></asp:label> </itemtemplate> </asp:templatefield>
for options refer: http://www.codedigest.com/articles/aspnet/137_how_to_format_datetime_in_gridview_boundcolumn_and_templatecolumn.aspx
Comments
Post a Comment