6

I am building a program with c#, and have included a datagridview component into it. The datagridview has a fixed amount of columns (2), which I want to save into two separate arrays. The amount of rows does change though. How could I do this?

3 Answers 3

11

Assuming a DataGridView named dataGridView1 and you want to copy the contents of the first two columns into Arrays of strings, you can do something like this:

string[] column0Array = new string[dataGridView1.Rows.Count];
string[] column1Array = new string[dataGridView1.Rows.Count];

int i = 0;
foreach (DataGridViewRow row in dataGridView1.Rows) {
    column0Array[i] = row.Cells[0].Value != null ? row.Cells[0].Value.ToString() : string.Empty;
    column1Array[i] = row.Cells[1].Value != null ? row.Cells[1].Value.ToString() : string.Empty;
    i++;
}
Sign up to request clarification or add additional context in comments.

8 Comments

@Abe of course if he was using .NET 2.0+ we'd recommend using List<T> rather than an Array or ArrayList.
True on my side, but I was under the impression that if you know the length of your collection before hand that is generally better/more efficient to use an Array (though the performance gain may not be noticeable).
Um, I don't know if this is a newbie mistake, I am quite new to c#, but this is giving me a NullReferenceException on the column0Array[i]... line when I try to run it.
@Xedfire Ah, the cell that's throwing that exception probably has a null value. I updated the code to handle this situation. In case the ?: operator looks new, check this: ?: Operator (C# Reference)
@Jay Wouldn't it have to be .NET 2.0+ if he's using a DataGridView? That wasn't introduced until .NET 2.0...
|
4

Try this:

ArrayList col1Items = new ArrayList();
ArrayList col2Items = new ArrayList();

foreach(DataGridViewRow dr in dgv_Data.Rows)
{
  col1Items.Add(dr.Cells[0].Value);
  col2Items.Add(dr.Cells[1].Value);
}

1 Comment

After first getting the error when using Jay's code, I tried this out - and it did actually work. But, if you say that Jay's is more efficient, I'll probably keep experimenting!
3

I used Jay's example and changed it to store all the rows in a single array for easy exporting. At the end you can easily use LogArray[0,0] to get the string from cell 0, column 0.

        // create array big enough for all the rows and columns in the grid
        string[,] LogArray = new string[dataGridView1.Rows.Count, dataGridView1.Columns.Count];

        int i = 0;
        int x = 0;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            while (x < dataGridView1.Columns.Count)
            {
                LogArray[i, x] = row.Cells[x].Value != null ? row.Cells[x].Value.ToString() : string.Empty;
                x++;
            }

            x = 0;
            i++; //next row
        }

I hope i helped someone with this, its my first time posting any code online, ever. Also i haven't coded in ages just getting started again.

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.