Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

How to display only Date part of a datetime data that is bind to a gridview control?

Hi, I'm using vs2012.
I have to convert one of the gridview column to a templatefield instead of a bounfield because i need to add validation control to it.  The regular expression validator is to make sure users enter correct date format during Editing mode.  However, I lost the DataFormat in the property.  I only want to display the date part.  The regular expression I have configure also only validates Date.  I also limit the max characters to be 10 in the Textbox.  However, the data bind to the column is datetime type and upon page load, it displays date and time, mm/dd/yyyy hh:mm:ss
Is there a way to fix this?  Thank you.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

try:
// code behind
private object FormatDate(DateTime input)
{
    return String.Format("{0:MM/dd/yy}", input);
}

// html
<asp:TemplateField HeaderText="DateFieldCaption">
        <ItemTemplate>
            <asp:Label ID="Label8" runat="server" 
                Text='<%# FormatDate(Eval("DateFieldName")) %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

Open in new window

Avatar of lapucca
lapucca

ASKER

Data bind is of DateTime? type so I just changed that in the FormatDate's parameter.

in my code, do I put "FormatDate(Eval("inside of "Bind"?  Like below?  Or would it work with Bind?  Thank you.

            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("DataFieldName") %>'></asp:Label>
            </ItemTemplate>
try a simpler solution if you prefer not to have code behind. I was about to change it but you beat me to it:

Text='<%# Eval("DataFieldName","{0:d}") %>'

re:>  would it work with Bind?
In these situation you are in advantage because you have the code to try it
Avatar of lapucca

ASKER

I like the Eval solution better.  However, it works when the grid is load up.  However, when I click on the Edit, in the edit mode the textbox is displayed with year and time.  

Either I have to find a regular expression that would validate both date and time or is there a easy way to have a calendar/date time picker control for this column of template field?  T

The data is datetime? type.  At minimum, user need to be able to edit the date if it's too complicate for them to edit time then that's fine.  But I need to make sure if no date time picker pop up then at lease a date/time regular expression is in the validator control working properly.  What would you suggest to do that would be the easiest way for this?
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

okay, will try it now.  thank you.
Avatar of lapucca

ASKER

Having a bit hard time following the url you provided.  I think it's time for my brain to reboot.  It's passed 11pm here.  

I asked for help with calendar earlier but I can't get it to work in my code.  I got a jQuery code here, https://www.experts-exchange.com/questions/28584726/How-to-have-a-calendar-popup-in-gridview-control-column-in-edit-mode.html 

I put the jQuery reference in the Site master file's header.  I then put the block of script at the end of the page content tag. However, no pop up calendar when I click on the grid in the edit mode.  it's just a text box for editing as usual.  I don't know if it's because I didn't put the corrent ClientID in the code but never really got answer to that.

<script type="text/javascript">
    jQuery(function ($) {
        $("#<%= myGrid.ClientID %>").on("click", "tr td:last-child text:last", function () {
            $(this).datepicker();
            $(this).datepicker("show");
        });
    });
</script>
</asp:Content>
I am in PST too. Next I will work at it will be in the morning.
Avatar of lapucca

ASKER

sounds good, thank you very much!
Avatar of lapucca

ASKER

Thank you.