繁体   English   中英

如何将datagridview的列的数据存储在int数组C#中?

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

我试过这个:

int[] b = new int[10];

foreach (DataGridView Row row in datagridview1.Rows) 
{
    b[i]=
}

我需要将列存储到 int 数组中,该列仅包含整数。

假设您想要第一列的值,该列的索引为 0。

PS:最好使用 for 而不是 for each,因为您想将值添加到数组中,而没有索引(或附加值),您将不得不编写更多代码。

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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