如何解决未设置为对象实例的对象引用

时间:2015-10-19 04:48:27

标签: c#

private void button1_Click(object sender, EventArgs e)    
{          
     richTextBox1.Clear();

     foreach (string line in richTextBox2.Lines)      
     {  

           string searchValue = line;
           searchValue = searchValue.Trim();
           dataGridView1.SelectionMode =DataGridViewSelectionMode.FullRowSelect;
            try
             {
              foreach (DataGridViewRow row in dataGridView1.Rows)
               {
                if(row.Cells[0].Value.ToString().Trim().Equals(searchValue))//error containing line
                 {
                   richTextBox1.Text += (row.Cells[1].Value.ToString().Trim()) + "\n";
                 }
               }
             }
             catch (Exception exc)
             {
                    MessageBox.Show(exc.Message);
             }
     }
}

1 个答案:

答案 0 :(得分:0)

foreach(dataGridView1.Rows中的DataGridViewRow行)

{

如果(row.Cells [0]。价值== NULL) {

中断;

} 别的{

if(row.Cells [0] .Value.ToString()。TrimStart()。TrimEnd()。Equals(searchValue.TrimStar t()。TrimEnd()))

{richTextBox1.Text + =(row.Cells [1] .Value.ToString()。TrimStart()。TrimEnd())+“\ n”;}