retrieve date from SQL into C# gridview - c#

I've sql database which is connected to c# windows form
in the database there's column with date , data type only date dd/mm/yyyy
and the data inside this column is also saved the same
I've a gridview in the C# which retrieve all the data into it, it shows the date asin database dd/mm/yyyy
I've a trigger " SelectionChanged" on the grid and it copies the data into textBoxes , but the date always comes with time dd/mm/yyyy tt:mm:ss AM/PM which is not in the Database at all
what I'm missing ?
checked the datatypein sql, it showed only dd/mm/yyyy no date in datatype or in thedata itself
reconnected the database to the C# application, recreated the gridview

You can use
dgv.Columns["DateColumnName"].DefaultCellStyle.Format = "dd/MM/yyyy"

Related

How to show database stored multiple dates in calendar control with different color

using Asp.net, c# and MS Sql Database created a Room Booking APP
I need the following Code in C#
I have database (rmBooking_tbl) with variable Checkindt and checkoutdt (NVARCHAR(50)) and stored room booking dates
Now I want to show these booked date ranges(Check in date to check out date) in calendar1 by highlighting with some different color and also should be disabled these dates for further selecting
disable all previous dates from current date
Please help me for creating code

Retrieve Data from DataGridview to textbox

I have created a Windows Application form in C# which has a datagridview control which name is datagridview1 and three text box which named txtTDate, txtTaka. i want to display the datagridview records to the text box when i select a row of datagridview. I successfully do that. But Problem is When i select a row in my datagridview textbox Tdate shows the date as dd/mm/yyyy HH:MM:SS Like 25/06/2018 00:00:00 Format. I want to the text box only date like 25/06/2018. How can i do that. Please anyone help. N.B: My database TDate field is DateTime.
I use the folling code to retrieve data to text box txtTDate and txtTaka:
TDate.text = dataGridView1.SelectedRows[0].Cells[TDate].Value.ToString();
Taka.text = dataGridView1.SelectedRows[0].Cells[Taka].Value.ToString();
TDate.text = ((DateTime)dataGridView1.SelectedRows[0].Cells[TDate].Value).ToString("dd/MM/yyyy");
Taka.text = ((DateTime)dataGridView1.SelectedRows[0].Cells[Taka].Value).ToString("dd/MM/yyyy");
Documentation on how to format DateTime can be found here.

Datatable and Date Column Manipulation

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.

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