2016-06-14 5 views
0

사용자가 아무 것도 선택하지 않고 빈 문자열로 바꿔서 comboBox에있는 경우를 감지하려면 어떻게해야합니까? 나는 comboBox 데이터 소스를 채우기 위해 사용합니다. comboBox가 null을 빈 문자열로 바꿉니다.

public List<Oferty1> FindOffer(string country, string accommodation, string transport, string maintenance) {...} 

또는

어떻게 다른 방법으로 내가이 경우 문자열에 널 (null)로 변환 할 수 있습니다 :
if (comboBoxTransport.SelectedItem.ToString() == null) 
      comboBoxMaintenance.SelectedItem = ""; 
     this.dataGridViewOffer.DataSource = soc.FindOffer(comboBoxCountry.SelectedItem.ToString(), comboBoxAccommodation.SelectedItem.ToString(), 
      comboBoxTransport.SelectedItem.ToString(), comboBoxMaintenance.SelectedItem.ToString()).ToList(); 

나는 제대로 메서드를 호출해야합니까?

답변

1

당신은 당신이 필요로하는 곳을 확인 Ternary Operator를 사용할 수 있습니다

comboBoxTransport.SelectedItem == null ? String.Empty : comboBoxTransport.SelectedItem.ToString() 

전체 코드 :

if (comboBoxTransport.SelectedItem == null) //ToString can not be called if property is null 
comboBoxMaintenance.SelectedItem = ""; 

this.dataGridViewOffer.DataSource = 
    soc.FindOffer(
        comboBoxCountry.SelectedItem == null ? String.Empty : comboBoxCountry.SelectedItem.ToString(), 
        comboBoxAccommodation.SelectedItem == null ? String.Empty : comboBoxAccommodation.SelectedItem.ToString(), 
        comboBoxTransport.SelectedItem == null ? String.Empty : comboBoxTransport.SelectedItem.ToString(), 
        comboBoxMaintenance.SelectedItem == null ? String.Empty : comboBoxMaintenance.SelectedItem.ToString() 
       ).ToList(); 
+0

좋아! 놀랍습니다! 정말 고맙습니다! :디 – Quicki

관련 문제