1

2 つの datetimepicker コントロールを持つ Windows フォームがあります。1 つは日付用で、もう 1 つは時刻用の datetimepicker コントロールです。これらのコントロールはどちらもデータベース内の同じ列にバインドされており、コントロールのプロパティ名 (dateEdit と timeEdit) とフォーマット (Long と Time) が異なります。

ここに私の問題/質問があります:

  1. timeEdit ピッカーは、データベース エントリ内の秒数を無視し、時間の秒数を「2:34:00」のように「00」に設定します。データベース エントリ (この図では時間に合わせて調整されています) は「14:34: 31.891123 -04:00". 秒を正しく表示するにはどうすればよいですか?
  2. 「2:34:15」のように、timeEdit ピッカーで秒を「00」から (たとえば)「15」に編集するたびに、ピッカーは値を次の関数に渡す前に秒を「00」にリセットします。正しい秒の値を渡すにはどうすればよいですか?
  3. 時間のミリ秒を編集したいと思います。トリミングされたミリ秒 (DATEPART を使用) をテキスト ボックスにバインドするのが最善ですか? ミリ秒をテキスト ボックスに正しく表示するには、ミリ秒を char または string に変換またはキャストする必要がありますか?

助けてくれてありがとう!

編集フォームをトリガーするコード:

    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;
4

2 に答える 2

0

DateTimePicker.CustomFormat プロパティを使用する必要があります

s The one- or two-digit seconds.

ss The two-digit seconds. Single digit values are preceded by a 0.

ミリ秒単位で DateTimePicker を使用することはできません。

于 2012-07-13T00:00:35.653 に答える
0

問題は解決しましたが、どうすればよいかわかりません。これが私がしたことです:

  1. calendarDataSet で、両方のクエリ (Fill,GetData と FillById,GetDataBy (@ID)) を更新して、calendarTime を CONVERT(VARCHAR(12), calendarTime, 114) AS calHoursMinsSec として選択しました。
  2. 要するに、時間、分、秒、ミリ秒を含む新しい列を作成しました
  3. フォームにテキスト ボックスを追加し、テキスト ボックスを calHoursMinsSec にバインドしました。

注: datetime を varchar に変換する以前の試みは、オペレーター エラーが原因で失敗したことは間違いありません。

フォームを保存すると、バインディングが固定されているように見え、関連する変数を更新関数に渡すことができました

皆様のご意見をお寄せいただきありがとうございます。ガイダンスと提案に感謝します!

于 2012-07-16T23:09:55.823 に答える