Datatable and Date Column Manipulation - c#

I am working on a database driven c# application.
I have a datatable that has a date column in it. Bind this datatable to datagridview and let the user edit the records and upon clicking save, save these changes in the database.
Date format in grid changes based on the system settings between dd/MM/yyyy and MM/dd/yyyy.
Data entry operators are not smart enough to look at this difference, so while editing the records they sometime move the records to different month.
Date Format is dd/MM/yyyy 10/10/2010
They want to change it to 12th October from 10th October. They made this change 10/12/2010 assuming the date format is MM/dd/yyyy.
There always remains this problem, so I want to fix the date format in the datatable.
What are the approaches I could use?

Use this code
var style = new DataGridViewCellStyle();
style.Format = "dd-MMM-yy";
colDate.DefaultCellStyle = style;
You don't need to convert it back and forth, it'll do all the things you want to achieve,

Are you setting the Column DataType as DateTime? I don't understand why this issue can occur. Internally, .NET should be saving this cell value as DateTime, and passing this to the database. Date formatting is not an issue.

Related

Force date format in datagridview

Good morning:
I have a Dgv with a cell that contains the Date.
In the Load of the Form I do the following:
dgvContratoRenov.Columns[14].DefaultCellStyle.Format = "d";
When executing the Project, the Date appears correctly (Ex: 05/04/2017).
My problem is when I am editing it does not respect the "/" and I must write the complete date. The idea is that it works as if it were a MaskedTextbox, that is, after entering the day skip to the position of the month and then the year.
It's possible? Thank you

Force cell format after setting value in ClosedXML

I have an interesting issue that I can't seem to figure out how to solve.
I need to export a date into excel in one format, but display it in another.
The user should see: ddMMMyyyy, but they enter it in MM/dd/yyyy. The problem I am having is that if I set the cell value to a MM/dd/yyyy format, it is not auto formatting to the ddMMMyyyy unless I manually edit the field in excel.
Is there any way to force the formatting?
worksheet.Cell("I" + rowIndex).SetValue<String>(LastUpdated.ToString("MM/dd/yyyy"));
worksheet.Cell("I" + rowIndex).Style.DateFormat.Format = "[$-409]dd-mmm-yyyy;#";

How to change Datetime format in a DataGridView cell

I am stucked about datagridview control in which i need to type datetime format in a cell like my own format i need.
I want from the client to enter datetime like "mm/dd" and hit enter, then this field will be seen as a short datetime format.
For example: Client will write "06/27" and hit enter. In cell, it will show 06/27/2015. (The year that we are in). Why i need is that, i will be able to send data in a format that in SQL server accepts.
Should i need also to use it with datetimepicker control or without it i can also handle it?
Well, if you really need to change the value of cell you can use dataGridView_CellEndEdit event and then access cell with:
var cell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];

C# TableAdapter Fills in with DateTime Field. how do i edit so only Time is shown in the table

My windows form project displays a table using the TableAdapter with the following Columns: Name, Location, Start Time, Category. The Start Time column corresponds to a DateTime field in the MS Visual Studio 2010 Dataset. It is redundant in the table to display MM/DD/YYYY HH:MM:SS AM/PM when the label above the table displays "Events for MM/DD/YYY". How can I display just the Time in the Table? The table automatically populates with the information and there is no way for me to say date.ToShortTimeString() before the info enters the table.
Set the format for the column to t.
For specific instructions, see the documentation for your grid control (or whatever you're using).
EDIT: In .Net's built-in DataGridView, you can set the column's DefaultCellStyle.Format property to "t".
For more information, see Standard Date and Time Format Strings.

Take the datepicker value

i have a gridview that dynamically generate datepicker , i have taken the value of datepicker using
GMDatePicker dtp = (GMDatePicker)Gridview1.Rows[rowIndex].Cells[3].FindControl("frmdate_starttime");
and i have inserted using dtp.Date,while storing in the database it only store the date and not the time. how to retrive the time to save in database along with date
You can change the Format property to Custom and the CustomFormat property to what you require, something like dd MMM yyyy hh:mm.
You should then be able to use
DateTime dt = dateTimePicker1.Value;
to get the date and time.
Split the problem into two parts: are you getting the right value out of the control but failing to save/restore it in the database correctly, or are you failing to get the right value out of the control in the first place? Once you've worked out whether it's a UI or database problem, you can ignore the other half of it.
Have you enabled the time picker in the control?
What column type are you using in the database?
If you run your application in the debugger, what value do you see before you store it in the database?
If you run a query against the database in the relevant admin tool, does that show a time?

Categories