从 datagraidview 获取 null 时如何避免错误
How avoid error when get null from datagraidview
这里我需要避免来自datagridview (winform)的null
private void SendUpdate()
{
if ((string)dataGridView1.SelectedRows[0].Cells[0].Value != string.Empty )
{
if (1 == dataGridView1.SelectedRows.Count)
{
int Id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
Update up = new Update();
up.AssignValue(Id);
up.textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
up.comboBox1.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
up.textBox2.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
up.textBox3.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
up.textBox4.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
up.textBox5.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
up.ShowDialog();
}
else
{
MessageBox.Show("Please Select the Single Data Which Required to Update");
}
}
else
{
MessageBox.Show("You Select the empty Row");
}
}
我试过 !=string.empty;
, !=null;
, !="";
但错误未解决。
1 == dataGridView1.SelectedRows.Count
是的
但是
(string)dataGridView1.SelectedRows[0].Cells[0].Value
有空值
显示错误
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.DataGridViewCell.Value.get returned null.
private void SendUpdate()
{
if (String.IsNullOrWhiteSpace(dataGridView1.SelectedRows[0].Cells[0].Value as string))
{
MessageBox.Show("You Select the empty Row");
}
}
不工作
它消除了所有具有数据的偶数行
这里我需要避免来自datagridview (winform)的null
private void SendUpdate()
{
if ((string)dataGridView1.SelectedRows[0].Cells[0].Value != string.Empty )
{
if (1 == dataGridView1.SelectedRows.Count)
{
int Id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
Update up = new Update();
up.AssignValue(Id);
up.textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
up.comboBox1.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
up.textBox2.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
up.textBox3.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
up.textBox4.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
up.textBox5.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
up.ShowDialog();
}
else
{
MessageBox.Show("Please Select the Single Data Which Required to Update");
}
}
else
{
MessageBox.Show("You Select the empty Row");
}
}
我试过 !=string.empty;
, !=null;
, !="";
但错误未解决。
1 == dataGridView1.SelectedRows.Count
是的
但是
(string)dataGridView1.SelectedRows[0].Cells[0].Value
有空值
显示错误
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.DataGridViewCell.Value.get returned null.
private void SendUpdate()
{
if (String.IsNullOrWhiteSpace(dataGridView1.SelectedRows[0].Cells[0].Value as string))
{
MessageBox.Show("You Select the empty Row");
}
}
不工作 它消除了所有具有数据的偶数行