Index Out Of Range Exception In C#

while Reading DataGridView

Page 1 of 1

3 Replies - 8000 Views - Last Post: 02 September 2010 - 12:53 AM Rate Topic: -----

#1 srinivas_ag  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 27-August 09

Index Out Of Range Exception In C#

Posted 01 September 2010 - 07:06 PM

Dear all,
I have a problem "Index Out Of Range Exception In C#" while reading Datgridview,

Actually i have a DataGridview. DataGridForm2 with 3 rows ,1 column..

Like .. Status
---------
completed
---------
completed
----------
" " ---- Empty string..


I tried to read this datgridview with the below code:


 int j =  dataGridForm2.Rows.Count;
             j = j - 1;


             for (int i = 0; i < j; i++)
             {
               


                object value1 =dataGridForm2.Rows[i].Cells[i].Value;
                            
                 }





But while second iteration that is i=1 , i got error Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


I dont know how to solve this problem, since i am fresher to .Net.

Please let me know my Error in above codings?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Index Out Of Range Exception In C#

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Index Out Of Range Exception In C#

Posted 01 September 2010 - 10:07 PM

If you have only 1 column, then when the i = 1 and you call .Cell[i], it can't read Cell[1] because it doesn't exist (only cell 0 because 1 column).

So change this:

object value1 =dataGridForm2.Rows[i].Cells[i].Value;


into this:

object value1 =dataGridForm2.Rows[i].Cells[0].Value;

Was This Post Helpful? 1
  • +
  • -

#3 Guest_pavankumar*


Reputation:

Re: Index Out Of Range Exception In C#

Posted 01 September 2010 - 10:23 PM

View Postsrinivas_ag, on 01 September 2010 - 06:06 PM, said:

Dear all,
I have a problem "Index Out Of Range Exception In C#" while reading Datgridview,

Actually i have a DataGridview. DataGridForm2 with 3 rows ,1 column..

Like .. Status
---------
completed
---------
completed
----------
" " ---- Empty string..


I tried to read this datgridview with the below code:


 int j =  dataGridForm2.Rows.Count;
             j = j - 1;


             for (int i = 0; i < j; i++)
             {
               


                object value1 =dataGridForm2.Rows[i].Cells[i].Value;
                            
                 }





But while second iteration that is i=1 , i got error Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


I dont know how to solve this problem, since i am fresher to .Net.

Please let me know my Error in above codings?

Thanks.



Hi,

"dataGridForm2.Rows[i].Cells[i].Value;"


but here number of rows <> to number of cells. so that u r getting this error.

u should keep

dataGridForm2.Rows[J].Cells[0].Value;

becuase, u r making loop for row (row's count) and actuall u should make 2 loops.

one for Row's and one Col's..

for(int i=0;i<datagridview.rows.count;i++)
{
for (int j=0;j<datagridview.cols.count;j++)
{
get value of == datagridform2.rows[i].cells[0].value;;......
}

}

try this..
Was This Post Helpful? 0

#4 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Index Out Of Range Exception In C#

Posted 02 September 2010 - 12:53 AM

Just an observation but it seems you get the count and take one off it, but then only use less than in your for loop. For example, objects that have a count of 5, you'd loop from 0 to less than 5, or in other words, 4. You use i < count because indexes are zero based and counting isn't. But because you're already taking one off the count, you need to use for (int i = 0; i <= j; i++) or simply remove j = j - 1; which can also be written as j--; by the way.

int j =  dataGridForm2.Rows.Count;
j = j - 1;
for (int i = 0; i < j; i++)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1