简体   繁体   中英

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) 
{
    b[i]=
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM