C#:从datagridview中选择某些行

时间:2012-04-20 07:20:58

标签: datagridview

        string[] column0Array = new string[dataGridView1.Rows.Count];
        string[] column1Array = new string[dataGridView1.Rows.Count];


        int k = 0;

        List<DataGridViewRow> rows = null;
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if (rows != null)
            if (i % 4 == 0)
                rows.Add(dataGridView1.Rows[i]);

            foreach (DataGridViewRow row in dataGridView1.Rows)
                // if (dataGridView1.Rows.IndexOf(i%4 == ) )


                {
                    column0Array[k] = row.Cells[0].Value != null ? row.Cells[0].Value.ToString() : string.Empty;
                    column1Array[k] = row.Cells[1].Value != null ? row.Cells[1].Value.ToString() : string.Empty;

                    k++;
                }
        }

这样的东西?我有一个未处理的例外......

1 个答案:

答案 0 :(得分:1)

List<DataGridViewRow> rows = null;
for (int i =0;i<dataGridView1.Rows.Count;i++)
{
  if(i%4==0)
   rows.Add(dataGridView1.Rows[i]);

  column0Array[i] = dataGridView1.Rows[i].Cells[0].Value != null ? dataGridView1.Rows[i].Cells[0].Value.ToString() : string.Empty;
  column1Array[i] = dataGridView1.Rows[i].Cells[1].Value != null ? dataGridView1.Rows[i].Cells[1].Value.ToString() : string.Empty;

}