How to programmatically set cell value in DataGridView?

前端 未结 14 887
無奈伤痛
無奈伤痛 2020-11-30 02:05

I have a DataGridView. Some of the cells receive their data from a serial port: I want to shove the data into the cell, and have it update the underlying bound object.

14条回答
  •  温柔的废话
    2020-11-30 02:54

    private void btn_Addtoreciept_Click(object sender, EventArgs e)
    {            
        serial_number++;
        dataGridView_inventory.Rows[serial_number - 1].Cells[0].Value = serial_number;
        dataGridView_inventory.Rows[serial_number - 1].Cells[1].Value =comboBox_Reciept_name.Text;
        dataGridView_inventory.Rows[serial_number - 1].Cells[2].Value = numericUpDown_recieptprice.Value;
        dataGridView_inventory.Rows[serial_number - 1].Cells[3].Value = numericUpDown_Recieptpieces.Value;
        dataGridView_inventory.Rows[serial_number - 1].Cells[4].Value = numericUpDown_recieptprice.Value * numericUpDown_Recieptpieces.Value;
        numericUpDown_RecieptTotal.Value = serial_number;
    }
    

    on first time it goes well but pressing 2nd time it gives me error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" but when i click on the cell another row appears and then it works for the next row, and carry on ...

提交回复
热议问题