Adds a new row of data to the DataGridView that can be added to the last row or as the first row

Source: Internet
Author: User

My development environment: Microsoft Visual Studio. NET 2005

This program is the Windows Forms application

Create a new Windows Forms Application project, open Form1, place a DataGridView control and button on the form, add two columns to the columns of DataGridView, and name Stuname ( "Name" is not available here because it conflicts with the name of the control) and age. The following figure:

The overall code is as follows:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;

Namespace Wind
{
public partial class Form1:form
{

int c = 0;

Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow ();
Dr. Createcells (DGV);
Dr. Cells[0]. Value = "H1";
Dr. CELLS[1]. Value = (++c);
DGV.     Rows.insert (0, Dr); The inserted data appears as the first line

DGV.                    Rows.Add (DR); The inserted data is displayed as the last line
}
}
}

Run by F5 to see the results of the run. The following are the results of the operation as the first row was inserted:

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.