Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,i want to DFS(Depth first search) my graph and add the number into Queue,so i have to access the nodes in same collumn (in DataGridView3) and put them in Queue(DataGridView5),if it has seen before then update them(line **//), i have a problem in line(**//),the error is:object refrence not sent to an instance of an object.
plz help me with this problem.tnx


C#
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);

dataGridView1.ColumnCount = a + 1;
dataGridView1.RowCount = b + 1;

dataGridView3.ColumnCount = (a * b) + 3;
dataGridView3.RowCount = (a * b) + 3;

dataGridView5.ColumnCount = (a * b) + 2;
dataGridView5.RowCount = 2;

for (int i = 0; i < dataGridView5.ColumnCount - 1; i++)
{
   for (int k = 2; k < dataGridView3.RowCount; k++)
   {
      for (int L = 2; L < dataGridView3.ColumnCount; L++)
      {
         int p = 0;

         for (int f = 0; f <= dataGridView1.RowCount - 1; f++)
         {
              if (k >= dataGridView3.RowCount)
                                break;
            dataGridView5.Rows[1].Cells[i].Value = dataGridView3.Rows[k].Cells[L + p].Value;

           
            p = p + dataGridView1.ColumnCount - 1;

            if (dataGridView5.Rows[1].Cells[i].Value != "0")
 
                   **//dataGridView5.Rows[1].Cells[i].Value=int.Parse(dataGridView5.Rows[1].Cells[i].Value.ToString()) +int.Parse( dataGridView5.Rows[1].Cells[i-1].Value.ToString());
                            
         }
      }
   }
}
Posted
Updated 3-May-14 0:38am
v4

you have set dataGridView5.RowCount = 2; but you access 3rd row in that error line dataGridView5.Rows[2], this is zero based index, so 2nd index means 3rd item. if you need 2nd item you change it as dataGridView5.Rows[1]
next error can occur when you access dataGridView3.Rows[k], in line 15 you are increasing k value, it can be increase more than the existing rows of dataGridView3, you may need to put validation.
C#
if (k >= dataGridView3.RowCount)
 break;
dataGridView5.Rows[1].Cells[i].Value = dataGridView3.Rows[k].Cells[L + p].Value;
 
Share this answer
 
Comments
nedas65 3-May-14 3:49am    
Datagridview5 shows Queue,i want to BFS my graph and add number(dataGridView3.Rows[k].Cells[L + p].Value )into queue ( dataGridView5.Rows[2].Cells[i].Value), so DataGridView5.rows[1] show the index of my queue,i want to add my number into DataGridView5.rows[2]
nedas65 3-May-14 4:06am    
I have to access all node in a column(DataGridView3) , and put them into queue(DataGridView5)
RowCount for dataGridView5 is 2, so this will have the data to the row index 0 and 1 only.

and in the line no.- 14 you are setting the value for row index 2, so it is showing error

Quote:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


so check the rowindex of dataGridView5, so change the index or rowcount of dataGridView5, as your requirement.
 
Share this answer
 
Comments
nedas65 3-May-14 4:14am    
tnx
hi,
i don't know what is the purpose of this code. but the error occurs due to trying access to rows and cell which not exists.

dataGridView5.Rows[2].Cells[i].Value = dataGridView3.Rows[k].Cells[L + p].Value;

here Rows[2] should be Rows[1] because datagridview5 is having 2 rows. so maximum index is 1 not 2.

and likewise datagridview3 cells and rows should not be exceed while you run your program.Carefully rewrite your program and remember the first index of a grid view is (0,0). not (1,1).

thanks. hope this would help.
 
Share this answer
 
Comments
nedas65 3-May-14 4:13am    
thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900