我有一個Windows窗體與2個datetimepicker控件:一個用於日期和一個單獨的datetimepicker控件的時間。這兩個控件都綁定到數據庫中的同一列,並且控件具有不同的屬性名稱(即dateEdit和timeEdit)和不同的格式(即Long和Time)。Databound DateTimePicker C#中的時間#
這裏是我的問題/問題:
- 的timeEdit選擇器會忽略任何秒都在數據庫條目,將其設定秒爲「00」,如「2點34分00秒」,即使數據庫條目(爲了這個插圖而修剪)是「14:34:31.891123 -04:00」。我如何獲得正確顯示的秒數?
- 每當我將timeEdit拾取器中的秒數從「00」編輯到(例如)「15」時(如在「2:34:15」中),拾取器將秒數重置爲「00」,然後將值到下一個功能。我如何傳遞正確的秒數值?
- 我想編輯當時的毫秒數。對我來說,最好是將修剪後的毫秒(使用DATEPART)綁定到文本框?我是否需要將毫秒轉換爲字符或字符串才能在文本框中正確顯示它們?
感謝您的幫助!
代碼觸發編輯表單:
private void timeDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 5)
{
EditTime editForm = new EditTime((Guid)timeDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
editForm.StartPosition = FormStartPosition.CenterScreen;
editForm.ShowDialog();
editForm.Close();
}
}
catch (Exception ex)
{
string msg = "Error: ";
msg += ex.Message;
throw new Exception(msg);
}
}
代碼形式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StatusManager
{
public partial class EditTime : Form
{
private Guid calendarId;
public EditTime()
{
InitializeComponent();
}
public EditTime(Guid Id)
{
InitializeComponent();
calendarId = Id;
}
public string GetConnectionString()
{
var connString = ConfigurationManager.ConnectionStrings["StatusManager.Properties.Settings.StatusConnectionString"].ConnectionString;
return connString;
}
private void UpdateCalendarItem(string dateEdit, string timeEdit, string note)
{
var conn = new SqlConnection(GetConnectionString());
const string UpdateStatusSql = @"UPDATE dbo.statuses SET
calendarTime = @timeOffset
notes = @note
WHERE PK_calendarUID = @PK_calendarUID";
try
{
SqlCommand cmd = new SqlCommand(UpdateSql, conn);
var param = new SqlParameter[3];
param[0] = new SqlParameter("@PK_calendarUID", calendarId);
//Convert date(s) to correct format
string dateTimeCombined = dateEdit + " " timeEdit;
DateTime timeConverted = Convert.ToDateTime(dateTimeCombined);
DateTimeOffset timeOffset = new DateTimeOffset(timeConverted);
param[1] = new SqlParameter("@timeOffset", timeOffset);
param[2] = new SqlParameter("@note", note);
foreach (SqlParameter t in param)
{
cmd.Parameters.Add(t);
}
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = "Error updating 'calendarItems': ";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
private void editTimeButton_Click(object sender, EventArgs e)
{
UpdateCalendarItem(dateEdit.Text, timeEdit.Text, notes.Text);
this.Close();
}
private void EditTime_Load(object sender, EventArgs e)
{
this.locationsTableAdapter.Fill(this.locationsDataSet.locations);
this.calendarTableAdapter.FillById(this.calendarDataSet.calendarItems, calendarId);
}
}
}
代碼實例化的DateTimePicker:
this.timeEdit.CustomFormat = "";
this.timeEdit.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.calendarBindingSource, "calendarTime", true));
this.timeEdit.Format = System.Windows.Forms.DateTimePickerFormat.Time;
this.timeEdit.Location = new System.Drawing.Point(385, 30);
this.timeEdit.Name = "timeEdit";
this.timeEdit.ShowUpDown = true;
this.timeEdit.Size = new System.Drawing.Size(89, 20);
this.timeEdit.TabIndex = 2;
你可以顯示你正在使用的代碼示例..?你怎麼實例化dtp控制。 – MethodMan 2012-07-12 21:32:13
根據DateTimePickerFormat [文檔](http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepickerformat),選擇「Time」時使用的格式由計算機在區域中設置和Windows中的語言設置。你能告訴我們你的短時間和長時間格式在Windows中嗎? – SuperOli 2012-07-12 21:45:21
此外,您可以嘗試將格式設置爲自定義並將其設置爲hh:mm:ss(未驗證的格式,從內存中)。 – SuperOli 2012-07-12 21:47:29