C#的DataGridView列到一个数组 [英] C# datagridview column into an array

查看:930
本文介绍了C#的DataGridView列到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立与C#程序,并包括一个DataGridView组件进去。在DataGridView中的列(2),我要保存为两个单独的数组的一个固定的量。行的数量不会改变,虽然。我怎么能这样做呢?

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?

推荐答案

假设一个名为dataGridView1的DataGridView中,你想前两列的内容复制到字符串数组,你可以做这样的事情:

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

这篇关于C#的DataGridView列到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆