How to programmatically set cell value in DataGridView?

前端 未结 14 877
無奈伤痛
無奈伤痛 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:50

    I searched for the solution how I can insert a new row and How to set the individual values of the cells inside it like Excel. I solved with following code:

    dataGridView1.ReadOnly = false; //Before modifying, it is required.
    dataGridView1.Rows.Add(); //Inserting first row if yet there is no row, first row number is '0'
    dataGridView1.Rows[0].Cells[0].Value = "Razib, this is 0,0!"; //Setting the leftmost and topmost cell's value (Not the column header row!)
    dataGridView1[1, 0].Value = "This is 0,1!"; //Setting the Second cell of the first row!
    

    Note:

    1. Previously I have designed the columns in design mode.
    2. I have set the row header visibility to false from property of the datagridview.
    3. The last line is important to understand: When yoou directly giving index of datagridview, the first number is cell number, second one is row number! Remember it!

    Hope this might help you.

提交回复
热议问题