DataGridViewRow row = new DataGridViewRow();
            DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
            textboxcell.Value = C.time;
              row.Cells.Add(textboxcell);
              DataGridViewTextBoxCell textboxcel2= new DataGridViewTextBoxCell();
              textboxcell.Value = C.temperature;
              row.Cells.Add(textboxcel2);
                 
              
                      dataGridView1.Rows.Insert(1, row);//这句代码保存提供的行索引超出范围。
参数名: rowIndex

解决方案 »

  1.   

    dataGridView1.Rows.Insert(0, row);
      

  2.   

    我写成0了还是不行,“不能向没有列的 DataGridView 控件添加行。必须首先添加列。”
      

  3.   

    //要加这两句,添加列
    this.dataGridView1.Columns.Add("1", "1");//两个参数,前者为列名,后者为headertext
    this.dataGridView1.Columns.Add("2", "2");DataGridViewRow row = new DataGridViewRow();DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
    textboxcell.Value = 1;
    row.Cells.Add(textboxcell);DataGridViewTextBoxCell textboxcel2 = new DataGridViewTextBoxCell();
    textboxcel2.Value = 2;//这里是2,是1的话,就覆盖了
    row.Cells.Add(textboxcel2);
    dataGridView1.Rows.Insert(0, row);//这里是0,索引从0开始。