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);
}
}
}
Asked
Active
Viewed 277 times
-4
-
6Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dotnetom Oct 19 '15 at 04:49
-
It's a common error due to `null`, Could you please tell which line is throwing the error? – sujith karivelil Oct 19 '15 at 04:50
-
if(row.Cells[0].Value.ToString().Trim().Equals(searchValue))//error containing line – dilhan Oct 19 '15 at 04:57
-
D.d.S Read the link @dotnetom gave you. – Rob Oct 19 '15 at 05:01
-
1Use Convert.ToString() when you are not sure if source string will have valid value. So that even if you pass null it will not throw exception. – Nikhil Vartak Oct 19 '15 at 05:03
-
thank "dotnetkid". i done it. but error is still having.. – dilhan Oct 19 '15 at 05:11
-
thank you all of u. i solved my error. foreach (DataGridViewRow row in dataGridView1.Rows) { if(row.Cells[0].Value==null) { break; } else { if (row.Cells[0].Value.ToString().TrimStart().TrimEnd().Equals(searchValue.TrimStart().TrimEnd())) { richTextBox1.Text += (row.Cells[1].Value.ToString().TrimStart().TrimEnd()) + "\n"; – dilhan Oct 19 '15 at 11:00
1 Answers
0
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(row.Cells[0].Value==null) {
break;
} else {
if (row.Cells[0].Value.ToString().TrimStart().TrimEnd().Equals(searchValue.TrimStart().TrimEnd()))
{ richTextBox1.Text += (row.Cells[1].Value.ToString().TrimStart().TrimEnd()) + "\n";}

dilhan
- 19
- 1
- 7