gpt4 book ai didi

C# datagridview 列到一个数组

转载 作者:太空狗 更新时间:2023-10-29 22:09:52 25 4
gpt4 key购买 nike

我正在用 C# 构建一个程序,并在其中包含了一个 datagridview 组件。 datagridview 有固定数量的列 (2),我想将其保存到两个单独的数组中。虽然行数确实发生了变化。我怎么能这样做?

最佳答案

假设有一个名为 dataGridView1 的 DataGridView,并且您想将前两列的内容复制到字符串数组中,您可以这样做:

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++;
}

关于C# datagridview 列到一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5585740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com