The simplest way
You can statically bind the data source so that the corresponding row is automatically added for the DataGridView Control.
If you need to dynamically add new rows to the DataGridView control, there are a number of ways to do this, and here's a quick introduction to two ways to add new rows dynamically for the DataGridView control:
Method One:
The code is as follows:
int Index=this.datagridview1.rows.add ();
This.datagridview1.rows[index]. Cells[0]. Value = "1";
This.datagridview1.rows[index]. CELLS[1]. Value = "2";
This.datagridview1.rows[index]. CELLS[2]. Value = "Listening";
Adds a new row to the DataGridView control with the DATAGRIDVIEW1.ROWS.ADD () event, which returns the index number of the new row, the line number of the new row, and then the individual cells of the row can be manipulated by that index number, such as Datagridview1.rows [index]. Cells[0]. Value = "1". This is a very common and very simple method.
Method Two:
The code is as follows:
DataGridViewRow row = new DataGridViewRow ();
DataGridViewTextBoxCell Textboxcell = new DataGridViewTextBoxCell ();
Textboxcell. Value = "AAA";
Row. Cells.add (Textboxcell);
DataGridViewComboBoxCell Comboxcell = new DataGridViewComboBoxCell ();
Row. Cells.add (Comboxcell);
DATAGRIDVIEW1.ROWS.ADD (row);
Method Two is more complicated than method one, but it is useful in some special situations, for example, to add controls such as drop-down boxes and buttons to some cells in a new row.
DataGridViewRow row = new DataGridViewRow (); Is the row object that creates the DataGridView, DataGridViewTextBoxCell is the contents of the cell is a TextBox,
DataGridViewComboBoxCell is the contents of the cell is a drop-down list box, in the same vein, Datagridviewbuttoncell is the contents of the cell is a button, and so on.
Textboxcell is the object of the newly created cell, and you can add its properties to the object. And then through row. Cells.add (Textboxcell) adds a Textboxcell cell to the Row object.
To add additional cells, use the same method.
Finally, add a new line row to the DataGridView1 control by DATAGRIDVIEW1.ROWS.ADD (row).
DataGridView the use of a control---add rows