C # datagridview 2 ways to add new rows _c# Tutorial

Source: Internet
Author: User

The data source can be statically bound 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 so, and here are two ways to dynamically add new rows to a DataGridView control:


Method One:

Copy Code code 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";

Use the DATAGRIDVIEW1.ROWS.ADD () event to add a new row for the DataGridView control that returns the index number of the new row, the row number of the newline, which can then manipulate the individual cells of the row, such as Datagridview1.rows [index]. Cells[0]. Value = "1". This is a very common and very simple method.


Method Two:

Copy Code code 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);

Methods The two methods are more complicated than others, but they are useful in special situations, for example, when you want to add a control such as a Drop-down box or a button 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, Datagridviewbuttoncell is the content of the cell is a button, and so on. Textboxcell is the object of a newly created cell, and you can add its properties to the object. And then through row. Cells.add (Textboxcell) adds Textboxcell cells to the row object. To add additional cells, use the same method.
Finally, a new row row is added to the DataGridView1 control by DATAGRIDVIEW1.ROWS.ADD (row).

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.