Kevin Rodriguez
Kevin Rodriguez

Reputation: 301

GridView Getting Out of Range Exception

I have a program that watches the text files in a network path folder whenever a text file is created it will be displayed on a Gridview as a path "@\192.168.10.5\Export" + the file name and after that it will loop through the Gridview then perform a insert query to database, if one text file is created, it works just fine, but if two text files are created, i'm getting Getting Out of Range Exception, which means it won't process the second text file, i don't know why?

Upvotes: 1

Views: 136

Answers (1)

DangeMask
DangeMask

Reputation: 551

If I understand it right:

  • The first foreach loop is considered to go through all rows
  • The second loop (for ... i < pathGrid.Rows.Count ...) should go through all cells in one row?

Why do you use row index as parameter in second loop, if you use it as cell index in rows.Cells[i] ?

I think here is the issue.

EDIT:

To explain my thought:

I don't know your datagrid structure, but i assume, each row contains the file path in first cell. So use only the first loop:

foreach (DataGridViewRow rows in pathGrid.Rows){
    string[] Lines = File.ReadAllLines(rows.Cells[0].Value.ToString());
.
.
.

Upvotes: 1

Related Questions