Modify Date/Time from SQLDataSource - c#

I have a date column in my database, it's not date time so the values have no time attached.
However, when I drag and drop a SQLDataSource on my page and bind a dropdownlist to it to display the date, it automatically adds a time of 12:00:00 AM.
I know why it does this, but I don't know how to get rid of it. Do I need to bind it from codebehind?
Is there an easy fix to this?

Set DropDownList.DataTextFormatString.
<asp:DropDownList
id="datesList"
runat="server"
dataTextFormatString="{0:dd/MM/yyyy}"/>
Then after DataBinding your control will apply the correct format:

Add format to field or column definition. For example Gridview:
<asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />
BTW, other method is to use ViewModel - class which is needed only for displaying data. In this class there is no need to use DateTime or other data types: only string. For example, you can define some displaying defaults here: if value null, show text "UNDEFINED" or what you need. In some cases this approach makes centralized point where you make preparation of data for display.

Can you show your sql query it may be useful .Have you use convert(varchar,date ,103) method to your query

C# doesn't have date datatype and though values from sql(date) datatype is convert to datetime field in c#. So better convert date as nvarchar in sql using convert(varchar,date ,103) though C# treats it as string.

Related

How to display DateTime as Date when Convert.ToDateTime() was run first

i'm stuck regarding a value I want to show. The problem is that I want to display in a GridView a Date value, but I can't parse it so it does that. I've tried to shrink the table column and several ways of parsing. Thank you so much for the help As it is now. And this is how it's parsed at the moment reserva.Fecha = Convert.ToDateTime(dr["fecha"].ToString());
DateTime object has both date and time associated with it. If you want to display only the date part of it, use the ToString() to only show the date with formatting defined. See Documentation here
DateTime.Parse(dr["fecha"].ToString()).ToString("MM-dd-yyyy");
You can also use the method ToShortDateString() to display only the Date part of the DateTime object.
DateTime.Parse(dr["fecha"].ToString()).ToShortDateString()
It turns out that the table I created previously was unable (I think) of displaying the data I had (in a List object) I still can´t display it from the list I want, but it shows the data I tell it to, so for now it works. Finally, instead of using <asp:TemplateField> <HeaderTemplate> <asp:Label for my table, I used <asp:BoundField>. I'm sorry if i'm not clear enough on the explanation, but I do believe it was a problem caused by using code I didn't understand so I don´t think it would happen to anyone else. Thank you so much everyone https://imgur.com/a/IWKHHvO

DeafultcCellStyle format not working on unbound DataGridView C# Winforms

I have an unbound DataGridView that I populate with data from an SQL database. One of the columns in the database is of type ‘datetime’.
On the load event of my winforms application, I use code below in order to format the column to only show date part of datetime column. However it shows the full datetime. I want 18/02/2020 and it’s showing 18/02/2020 00:00:00
dgv.Columns["Date"].DefaultCellStyle.Format = "dd/MM/yyyy"
dgv.Columns["Date"].ValueType = typeof(System.DateTime);
How do I format the column to only show the date portion of the datetime column?
I added some data in the gridview within my code and tried your code above. The DefaultCellStyle.Format works well.
Which means that you might need to check the way you're retrieving the data from your SQL Database. You may be filling your grid with strings instead of "dates" for example.

How to make a date column in a gridview not show timestamp when edited

I have searched and searched and have not found a clear answer for this issue:
When I set a column as a date column in a GridView and go into the grid and edit a date, it by default leaves a generic TimeStamp after the date like this (09/12/2017 00:00:00).
How do I remove the TimeStamp?
Make sure you have formatting string like this ({0:d}) and have set apply format in edit mode true like this (ApplyFormatInEditMode="true").
And implement to BoundField like this:
<asp:BoundField DataField="DataDate" ApplyFormatInEditMode="true" DataFormatString="{0:d}"
SortExpression="DataDate" HeaderText="Header Date" />

Two CalendarExtenders?

I have a popup ajax calendarextender that populates a text box with the selected date, (MM/dd/yyyy). This works. I want to capture the same date in an additional text box with a different format (yyyyMMdd). The first is for display and the second date is a parameter for gridview query. Is this done with two calandarextenders? (btw I'm using C#)
If you are supplying one to a query, you could just change the parameter value in code before supplying it to the query.
var datetimeForQuery = extenderVariable.SelectedDate.ToString("yyyyMMdd");

get the value of date picker used in gridview

i have create dynamic datepicker inside gridview. i want to know how to get the values of the datepicker
help me out!!
i have used this line
DateTime date2 = DateTime.Parse(((TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("frmdate_endtime")).Text);
Why not cast the control to the appropriate date/time picker class and take the value from that directly? You shouldn't need to parse the text.

Categories

Resources