Possible errors :
whenever u try to access an object index,, please make sure that the object is not null...
try adding this line
if( dt != null )
above
this.dataGridView1.DataSource = dt.DefaultView;
try adding this line
if( rc != null && rc.count > )
if( rc[0].cells.count > 0)
itemcode = rc[0].Cells["F1"].Value.ToString();
if( dataGridView2 != null && datagridview2.rows.count > 0)
for (int i = 0; i < dataGridView2.Rows.Count; i++)
if( datagridview2.rows[i].cells.count >2 )
if (dataGridView2.Rows[i].Cells[2].Value.ToString() == itemcode)