简体   繁体   中英

Winform DataGridView UserAddedRow to add a new row

It is possible to use event UserAddedRow to add a new row? When it is, do you have an example?

Thanks you in advance.

UserAddedRow event is fired after row added. You can not use it to add something, as it fires after somethign already was added.

According to msdn:

Occurs when the user has finished adding a row to the DataGridView control.

If you are not using data binding, you have to add the columns and rows manually.

dgvApplications.Columns.Add("Col1", "Column 1");
dgvApplications.Columns.Add("Col2", "Column 2");
dgvApplications.Rows.Add("Col1Value", "Col2Value");

But data binding is usually the better option.

BindingList<Application> applications = new BindingList<Application>();
dgvApplications.DataSource = applications;
// set AutoGenerateColumns to false and manually add columns to get pretty column names.
// set AutoGenerateColumns to true to not worry about adding columns in early prototyping
dgvModules.AutoGenerateColumns = true;  

Then if Application implements INotifyPropertyChanged you can do something like this:

applications.Add(new Application{ File="blah.txt" };

The property changed notification will allow the grid to automatically update when the underlying model is changed.

The data binding option helps decouple your GUI from your data. "Model-View-Controller" design pattern helps you later if you need to redesign the GUI, or add a secondary interface such as the web or phone app. http://msdn.microsoft.com/en-us/library/ff649643.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM