DataGridView the use of a control---add rows

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.