简体   繁体   中英

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.

Use an if statement instead:

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.

But just for fun: (return value "OK", "Cancel" etc when Message Box is shown)

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())();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM