You're using dataGridView1.RowCount which will return the total count of rows. Now, assuming that the value 348 is correct, always remember that while trying to fetch a value from a collection, like this, whose index are zero based, the last value can be accessed as follows:
txtid.Text =dataGridView1.Rows[(dataGridView1.RowCount - 1)].Cells[348].Value.ToString;
For ex in a collection, ABC of length 10, if you write ABC[ABC.Length], it means you're tring to access the 11th index of the collection which is not there, hence this error.