我在windowform中用datagridview创建了一个表格,建了三个列a,b,c
a   b  c
现在我通过自己输入数据,比如:
a   b   c
4   5   6
1   2   3
5   6   2
.........
现在我想知道如何获取b列中的数据,然后将这些数据放到一个数组中,请问各位高手要怎么实现?

解决方案 »

  1.   

     List<string> _str= new List<string> ();
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    _str.Add(dr.Cells["b"].Value.ToString());
                }
      

  2.   

    楼上的可以。
    也可以这样
      List <string>   _str=   new   List <string>   ();
    for(int rowIndex = 0; rowIndex<dataGridView1.Rows.Count;rowIndex++)
    {
        _str.Add((dataGridView1.Item("b",rowIndex).Value.ToString());
    }
      

  3.   

    如果将这些数据放到StringCollection中,那么最后可以将stringCollection中的值复制到一个string数组中,但是stringCollection中的数据是随时可以变化的,那么要如何定义String数组的范围,或者有没有其他办法?
    比如
    string[] str = new string[1024];
    stringCollection strco = new stringCollection();
    strco.CopyTo(str,0);
    总觉得其中的1024不好
      

  4.   

    string[]   str   =   new   string[dataGridView1.Rows.Count]; 
      

  5.   

      string[]       str       =       new       string[dataGridView1.Rows.Count];   
    for(int   rowIndex   =   0;   rowIndex <dataGridView1.Rows.Count;rowIndex++) 

            str[rowIndex]=dataGridView1.Item("b",rowIndex).Value.ToString(); 

      

  6.   

     string[] str = new string[this.dataGridView1.Rows.Count]; 
     for(int rowIndex = 0;rowIndex < this.dataGridView1.Rows.Count;rowIndex++)   
    {   
         str[rowIndex]=this.dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();   
    }