2013-10-11 2 views
1

DataGridViewDateTimePicker을 사용하여 사용자 지정 일정 컨트롤이있는 일부 코드를 수정합니다. 나는 ShowCheckBox = true을 설정하고 사용자가 체크 박스를 클릭하면 (날짜의 CustomFormat 변경) null로 날짜의 값을 변경하도록하고있다. 내 문제는 DateFormat을 변경하기 위해 확인란을 너무 많이 클릭해야한다는 것입니다. 내 기본 CalendarColumn은 http://msdn.microsoft.com/en-us/library/7tas5c80.aspx에서 왔지만 확인란을 포함하지 않았고 null 값을 허용하지 않았습니다. "날짜 밖으로DateTimePicker에서 여러 번 클릭하여 확인란을 눌러 OnValueChanged를 호출해야합니다.

protected override void OnValueChanged(EventArgs eventargs) 
    { 
     // Notify the DataGridView that the contents of the cell 
     // have changed. 
     base.OnValueChanged(eventargs); 

     if (this.Checked) 
     { 
      this.Checked = true; 
      this.Format = DateTimePickerFormat.Short; 
     } else if (!this.Checked) 
     { 
      this.Format = DateTimePickerFormat.Custom; 
      this.CustomFormat = " "; 
     } 
    } 

(이 값이 선택되어 있어요) 체크 박스의 첫 번째 클릭 회색,하지만 OnValueChanged() 방법을 발생하지 않습니다, 두 번째 클릭으로 다시 설정합니다

OnValueChanged() 코드는 checked "로 설정하고 이벤트를 발생시키고 세 번째 클릭은 CustomFormat을" "로 설정하므로 null 날짜가 표시됩니다.

나는 몇 가지 조사를 완료했습니다, 나는 내 문제는 첫 번째 클릭에 셀의 초점을 얻고 함께 할 수있는 뭔가이라고 생각하지만, 나는 onGotFocus()에 수표를 넣어 경우, 단 한번의 클릭이 표시됩니다 /가 숨기 날짜 형식을 변경해야하지만 체크 박스를 선택 해제 한 후에 다른 셀을 클릭하면 DateTimePickerFormat.Short이 아닌 CustomFormat이 ""으로 설정되어 있습니다.

비슷한 주제에 대한 다른 답변은 http://www.mofeel.net/70-microsoft-public-dotnet-framework-windowsforms/8806.aspx을 참조하십시오. 그러나이 코드를 어떻게 코드에 통합 할 것인지 혼란 스럽습니다. 나는 전체 수업을 게시하지 않았지만 누군가 도움이된다고 생각하면 할 수 있습니까?

답변

1

동료가이 문제를 해결했습니다. 내가 설명하려고합니다 최선 I 할 수 있습니다

OnValueChanged() 방법처럼 보이는 결국

:

:

protected override void OnClick(EventArgs e) 
{ 
    isChecked(); 
    base.OnClick(e); 
}  

public object EditingControlFormattedValue 
{ 
    get 
    { 
     if (!this.Checked) 
     { 
      return String.Empty;      
     } 
     else 
     { 
      if (this.Format == DateTimePickerFormat.Custom) 
      { 
       return this.Value.ToString(); 
      } 
      else 
      { 
       return this.Value.ToShortDateString(); 
      } 
     } 
    } 
    set 
    { 
     string newValue = value as string; 
     if (!String.IsNullOrEmpty(newValue)) 
     { 
      this.Value = DateTime.Parse(newValue); 
     } 
    } 
} 

InitializaEditingControl도에 편집 한 :

protected override void OnValueChanged(EventArgs eventargs) 
{ 
    valueChanged = true; 
    // Notify the DataGridView that the contents of the cell 
    // have changed. 

    this.EditingControlDataGridView.NotifyCurrentCellDirty(true); 
    base.OnValueChanged(eventargs); 

    isChecked(); 
} 

public bool isChecked() 
{ 
    bool isChecked = false; 
    if (this.Checked) 
    { 
     this.Checked = true; 
     this.Format = DateTimePickerFormat.Short; 
     isChecked = true; 
    } 
    else if (!this.Checked) 
    { 
     this.Format = DateTimePickerFormat.Custom; 
     this.CustomFormat = " "; 
     isChecked = false; 
    } 
    return isChecked; 
} 

그는 이러한 방법을 추가

public override void InitializeEditingControl(int rowIndex, object 
     initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
{ 
    // Set the value of the editing control to the current cell value. 
    base.InitializeEditingControl(rowIndex, initialFormattedValue, 
     dataGridViewCellStyle); 
    ctl = DataGridView.EditingControl as CalendarEditingControl; 
    // ctl.Invalidated += new InvalidateEventHandler(ctl_Invalidated); 
    ctl.ValueChangedSpecial += new EventHandler(ctl_ValueChangedSpecial); 
    if (rowIndex >= 0) 
    { 
     try 
     {      
      if (String.IsNullOrEmpty(this.Value.ToString())) 
      { 
       ctl.Checked = false; 
       ctl.Format = DateTimePickerFormat.Custom; 
       ctl.CustomFormat = " "; 
      } 
      else 
      { 
       ctl.Checked = true; 
       ctl.Value = (DateTime)this.Value; 
       ctl.Format = DateTimePickerFormat.Short;    
      } 
     } 
     catch (ArgumentOutOfRangeException aex) 
     { 
      //MessageBox.Show("ERROR. " + aex.Message); 
      ctl.Value = (DateTime)this.DefaultNewRowValue; 
     } 
    } 
} 

그리고 나서 효과가있었습니다.

이 답변은 거의 확실하지만 만족스럽지 않은 질문은 아무도 도와주지 않으므로 여기있는 내용이 다른 사람을 도울 수 있습니다.