簡體   English   中英

字符串和系統窗口表單對話框之間沒有隱式轉換

[英]no implicit conversion between string and system windows form dialog

我出錯了。 我喜歡顯示消息對話框,如果單擊按鈕后單元格為空

 var name = dataGridView1.Rows[0].Cells[1].Value != null ? dataGridView1.Rows[0].Cells[1].Value.ToString() : MessageBox.Show("Cell is empty") ;

您不能那樣做,因為文檔說:

first_expression和second_expression的類型必須相同 ,或者必須存在從一種類型到另一種類型的隱式轉換。

使用if語句代替:

string name;
if(dataGridView1.Rows[0].Cells[1].Value == null)
{
   MessageBox.Show("Cell is empty");
}
else
{
   name = dataGridView1.Rows[0].Cells[1].Value.ToString();
}

請永遠不要這樣做,Selman22的答案應該是正確的方法。

只是為了好玩:(顯示消息框時,返回值“ OK”,“ Cancel”等)

var name = dataGridView1.Rows[0].Cells[1].Value != null ? dataGridView1.Rows[0].Cells[1].Value.ToString() : new Func<string>(() => MessageBox.Show("Cell is empty").ToString())();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM