ccgt ccgt - 10 months ago 34
C# Question

How store the data of a column of a datagridview in an int array C#?

I tried this:

int[] b = new int[10];

foreach (DataGridView Row row in datagridview1.Rows)

I need store the column into int array, the column contains only integers.


Let's say you want the values of the first column, which will have the index 0.

PS : It's better to do it with a for rather than a for each, since you want to add the values into an array, without an index (or an additional value) you'll have to write more code.

int[] b = new int[datagridview1.Rows.Count];
for (int i = 0; i < datagridview1.Rows.Count; i++)
    b[i] = datagridview1.Rows[i].Cells[0].Value == null ? -1 : Convert.ToInt32(datagridview1.Rows[i].Cells[0].Value);
    // the ?: is to check weither the value is null or not, then asign -1 if it's null