简体   繁体   English

字符串和系统窗口表单对话框之间没有隐式转换

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

I am getting error. 我出错了。 I like to show message dialog if cell is empty after click a button 我喜欢显示消息对话框,如果单击按钮后单元格为空

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

You can't do it like that.Because the documentation says: 您不能那样做,因为文档说:

Either the type of first_expression and second_expression must be the same , or an implicit conversion must exist from one type to the other. first_expression和second_expression的类型必须相同 ,或者必须存在从一种类型到另一种类型的隐式转换。

Use an if statement instead: 使用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();
}

Please do not ever do this, Selman22's answer should be the way to go. 请永远不要这样做,Selman22的答案应该是正确的方法。

But just for fun: (return value "OK", "Cancel" etc when Message Box is shown) 只是为了好玩:(显示消息框时,返回值“ 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