我在windowform中用datagridview创建了一个表格,建了三个列a,b,c
a b c
现在我通过自己输入数据,比如:
a b c
4 5 6
1 2 3
5 6 2
.........
现在我想知道如何获取b列中的数据,然后将这些数据放到一个数组中,请问各位高手要怎么实现?
a b c
现在我通过自己输入数据,比如:
a b c
4 5 6
1 2 3
5 6 2
.........
现在我想知道如何获取b列中的数据,然后将这些数据放到一个数组中,请问各位高手要怎么实现?
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
_str.Add(dr.Cells["b"].Value.ToString());
}
也可以这样
List <string> _str= new List <string> ();
for(int rowIndex = 0; rowIndex<dataGridView1.Rows.Count;rowIndex++)
{
_str.Add((dataGridView1.Item("b",rowIndex).Value.ToString());
}
比如
string[] str = new string[1024];
stringCollection strco = new stringCollection();
strco.CopyTo(str,0);
总觉得其中的1024不好
for(int rowIndex = 0; rowIndex <dataGridView1.Rows.Count;rowIndex++)
{
str[rowIndex]=dataGridView1.Item("b",rowIndex).Value.ToString();
}
for(int rowIndex = 0;rowIndex < this.dataGridView1.Rows.Count;rowIndex++)
{
str[rowIndex]=this.dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
}