繁体   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