1

hi guys im new here =)

i would need your help in a minimal troubble case, to save time for me =)

The problem is about that, i want to have datagridview Cells Values and save the values to an array.

Up to now it works fine, but it just reads the first row cell and after that process it just tells that the index is out of bound, but i dont understand why.

foreach (DataGridViewRow row in dataGridView1.Rows)     //wo soll er sich aufhalten -> zeilen
                {

                    **if (row.Cells[i].Value == null)**
                    {
                        MessageBox.Show("This row is empty");
                        break;
                    }
                        if (row.Cells[i].Value != null)
                        {
                            UnsortArray[i] = row.Cells[i].Value.ToString();
                            MessageBox.Show(UnsortArray[i]);
                            ++i;
                        }

                }
4
  • What is "i". You are using a foreach loop to loop through all the rows.You need to loop through columns as well. Commented Sep 24, 2012 at 6:32
  • Because it tries to read cell[length] on the next row, which is out of bounds. Use two variables for index, one which you reset to 0 on every row. Commented Sep 24, 2012 at 6:33
  • Because i keeps getting bigger? Commented Sep 24, 2012 at 6:34
  • you want to store only one cell value in Array or all cell value ? In your code you mixup index 'i'. 'i' used as cell index as well as UnsortArray index. Commented Sep 24, 2012 at 6:44

4 Answers 4

2
    foreach (DataGridViewRow row in dataGridView1.Rows)   
                    {
                      for (int i=0; i<= dataGridView1.Columns.Count; i++)
{
                        **if (row.Cells[i].Value == null)**
                        {
                            MessageBox.Show("This row is empty");

                        }
                            if (row.Cells[i].Value != null)
                            {
                                UnsortArray[i] = row.Cells[i].Value.ToString();
                                MessageBox.Show(UnsortArray[i]);

                            }

    }



                    }

also, I don't understand why would you want to do messageBox.show inside a loop. It should be out side the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Why you increase i value in if (row.Cells[i].Value != null) block? for loop already increasing i value.
2
foreach (DataGridViewRow row in dataGridView1.Rows)     //wo soll er sich aufhalten -> zeilen
                {

                    if (row.Cells[i].Value == null)
                        MessageBox.Show("This row is empty")
                    else
                        {
                            UnsortArray[i] = row.Cells[i].Value.ToString();
                            MessageBox.Show(UnsortArray[i]);
                   //    ++i;  dont need to increment if you want to read only specific column
                        }
                }

2 Comments

Thank you very much, you solved my problem in the fastes possible way =) I tried to vote up, but its not possible yet! Do it afterwards!
@Niko: I will do it on behalf of you now
1

Try something like following code>>

for(int i=0;i<dataGridView1.Rows.Count;i++)
                {

                    if (dataGridView1.Rows[i].cells[i].Value==null)**
                    {
                        MessageBox.Show("This row is empty");
                        break;
                    }

                    else
                        {
                          UnsortArray[i] = dataGridView1.Rows[i].cells[i].Value.ToString();
                            MessageBox.Show(UnsortArray[i]);

                        }





                }

Comments

1

Use different Index variable for UnSortArray (let say j). i is a index of cell which value you want to store in UnSortArray

CODE

            foreach (DataGridViewRow row in dataGridView1.Rows)     
            {

                if (row.Cells[i].Value == null)
                {
                    MessageBox.Show("This row is empty")
                }
                else
                    {
                        UnsortArray[j] = row.Cells[i].Value.ToString();
                        MessageBox.Show(UnsortArray[j]);
                        ++j;
                    }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.