Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何避免从datagraidview获取null时出错_C#_Visual Studio 2012 - Fatal编程技术网

C# 如何避免从datagraidview获取null时出错

C# 如何避免从datagraidview获取null时出错,c#,visual-studio-2012,C#,Visual Studio 2012,这里我需要避免datagridview(winform)中的null 我试过=string.empty,=无效,=""; 但错误并没有解决 1==dataGridView1.SelectedRows.Counttrue 但是 (字符串)dataGridView1.SelectedRows[0]。单元格[0]。值具有空值 显示错误 System.NullReferenceException:“对象引用未设置为对象的实例。” System.Windows.Forms.DataGridViewCel

这里我需要避免datagridview(winform)中的null

我试过
=string.empty
=无效
="";
但错误并没有解决


1==dataGridView1.SelectedRows.Count
true

但是

(字符串)dataGridView1.SelectedRows[0]。单元格[0]。值
具有空值 显示错误

System.NullReferenceException:“对象引用未设置为对象的实例。”

System.Windows.Forms.DataGridViewCell.Value.get返回空值

它不起作用了
它消除了所有偶数行数据

您应该执行的操作
dataGridView1.SelectedRows[0]。Cells[0]。Value.ToString()
1==dataGridView1.SelectedRows.Count
,该操作应该位于外部if语句中。您需要首先检查是否有选中的行<代码>(字符串)dataGridView1.SelectedRows[0]。单元格[0]。值!=string.Empty
不再需要,除非您有意这样做,或者在某些情况下它可能为null.if(dataGridView1.SelectedRows[0]。单元格[0]。Value.ToString()!=string.Empty)System.NullReferenceException:“对象引用未设置为对象的实例。”System.Windows.Forms.DataGridViewCell.Value.get返回null。然后尝试此操作,(dataGridView1.SelectedRows[0]。Cells[0]。Value?.ToString()!=string.Empty)
 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");

            }
            
        }
 private void SendUpdate()
        {

            if (String.IsNullOrWhiteSpace(dataGridView1.SelectedRows[0].Cells[0].Value as string))
            {
                MessageBox.Show("You Select the empty Row");
            }
}