2012-03-22 14 views
2

タグごとに、これはEntity Framework、C#、Winformsの質問です。バインドされたdatetimeに空白のテキストボックス値をnullとして渡す方法

私は、自分のエンティティのnull可能な日時フィールドにバインドされたテキストボックスデータを持っています。私は、テキストボックスの内容を削除して空白のままにすると、エンティティにヌル値を戻したいと思います。

テキストボックスCausesValidationプロパティはtrueです。テキストボックスの内容を削除すると、有効な日付を入力せずにそのテキストボックスを残すことができません。

は、ここに私の検証イベント

private void txtDueDateDetail_Validating(object sender, CancelEventArgs e) 
{ 
    string errorMsg; 
    if (!ValidDate(txtDueDateDetail.Text, out errorMsg)) 
    { 
     // Cancel the event and select the text to be corrected by the user. 
     e.Cancel = true; 
     txtDueDateDetail.Select(0, txtDueDateDetail.Text.Length); 

     // Set the ErrorProvider error with the text to display. 
     this.epNew.SetError(txtDueDateDetail, errorMsg); 
    } 

    Debug.Write("text: " + txtDueDateDetail.Text); 
} 

public bool ValidDate(string pTextDate, out string errorMessage) 
{ 
    DateTime tempDate; 
    errorMessage = ""; 

    if (pTextDate.Length == 0) 
    { 
     //pass a null date...how? 
     return true; 
    } 

    DateTime.TryParse(pTextDate, out tempDate); 
    if (tempDate == DateTime.MinValue) 
    { 
     errorMessage = "date must be in format MM/dd/yyyy"; 
     return false; 
    } 

    return true; 
} 

任意のアイデアが参考になるです。

+0

TextBoxのtextプロパティがnullでないため、txtDueDateDetail.Text = nullという行はわかりません。値をすぐに読み取った場合は、空でない文字列が返されます。私は別のプロパティ(例えばTag)をバインドしようとし、バリデーションイベントでそのプロパティを更新しますが、EFでの作業方法がわからないのでここでは止めます。投稿に感謝します。 – Steve

答えて

1

私はnull datetime検証を回避できませんでした。したがって、[CausesValidation = FALSE]フィールドの検証を無効にし、Leaveイベント内で独自の検証を実行しました。

public bool ValidDate(string pTextDate, out DateTime? pDate, out string errorMessage) 
    { 
     DateTime tempDate; 
     errorMessage = ""; 
     pDate = null; 
     if (pTextDate.Length == 0) 
     { 
      //pass null date here... 
      return true; 
     } 
     DateTime.TryParse(pTextDate, out tempDate); 

     if (tempDate == DateTime.MinValue) 
     { 
      errorMessage = "date must be in format MM/dd/yyyy"; 
      return false; 
     } 
     pDate = tempDate; 
     return true; 
    } 

    private void txtDueDateDetail_Leave(object sender, EventArgs e) 
    { 
     string errorMsg; 
     DateTime? outDate; 
     if (!ValidDate(txtDueDateDetail.Text, out outDate, out errorMsg)) 
     { 
      txtDueDateDetail.Select(0, txtDueDateDetail.Text.Length); 
     } 
     else 
     { 
      int CID = Convert.ToInt32(txtChargebackIDDetail.Text); 
      var temp = (from c in chg.ChargeBackks 
         where c.ID == CID 
         select c).FirstOrDefault(); 
      temp.DueDate = outDate; 
     } 
     this.epNew.SetError(txtDueDateDetail, errorMsg); 

    } 

機能的ではありませんが、最適な解決策はありません。

2

最初のアプローチを考えて、Binding.NullValueに空の文字列を設定しようとしましたか?次のようにプログラム的にそれを行うことができます。NullValueに空の文字列を代入docsを1として

txtDueDateDetail.DataBindings["Text"].NullValue = ""; 

は、(そのデフォルト値である)nullを割り当てることと同じではありません。

あなたの最終的な解決策は問題ありません。私はNullValueプロパティについて読む前に実装を検討しました。私の欠点は、データバインディングの有用性が減ることです。変更された値を手動でデータソースに割り当てることになります。

+0

私はこのMS How Toもよく見つけました。 [方法:WindowsフォームコントロールをDBNullデータベース値にバインドする](http://msdn.microsoft.com/en-us/library/y0h25we8.aspx)このセクションを編集する必要がある場合は、上記の解決策。 –