Use the datagri to add and edit data

Source: Internet
Author: User

The datagridview control is often used to display data tables. The traditional method of adding and editing a data refresh the datagridview after adding data, or select a data item for editing and saving. This is very common and useful for maintaining a data table. However, if there is a lot of data to be edited in one interface, there may be various controls such as Textbox, dropdownlist, and datagridview, we need to edit and save the data. In this case, we want to edit all the data and save it together. This requires that the datagridview control can be built in to add and edit data, like excel, you can add, edit, or delete all rows and columns before saving data.

You can also use the datagridview in C #. net.

 

1. First, set the attributes of the datagridview:

Dgv. allowusertoaddrows = true;

Dgv. allowusertodeleterows = true; and so on;

Edit the columns to be displayed. At this time, the datagridview will automatically create new rows, and we can add several new rows of data;

* Save data:

Private void button#click (Object sender, eventargs e) <br/>{< br/> dgv. endedit (); <br/> foreach (maid R in dgv. rows) <br/>{< br/> If (! R. isnewrow) <br/>{< br/> pm_title Pt = new pm_title (); <br/> PT. contents = R. cells ["contents"]. value. tostring (); <br/> TDC. pm_title.insertonsubmit (PT); <br/>}< br/> TDC. submitchanges (); <br/>}< br/>

I use LINQ to implement interaction with the database. Before saving, execute endedit (). Otherwise, the data that has just been filled out but has not lost its focus cannot be found.

 

2. Existing data to be bound

If you directly bind data to the datagridview, allowusertoaddrows will become ineffective and will not automatically create new rows. Bindingsource can be used for data binding:

Bindingsource BS = new bindingsource (); <br/> private void gridbind () <br/>{< br/> List <pm_title> PTS = TDC. gettitles (datid); <br/> If (PTS. count> 0) <br/>{< br/> BS. datasource = PTS; <br/> BS. allownew = true; <br/> dgv. datasource = BS; <br/>}< br/>}

Set BS. allownew = true. After binding data, the new row will automatically appear. You can edit the original data or add new data.

After editing, You need to delete the original data when saving the data, and then insert the data in the table row by row:

Private void button#click (Object sender, eventargs e) <br/>{< br/> dgv_ptitle.endedit (); <br/> List <pm_title> PTS = TDC. gettitles (datid); <br/> TDC. pm_title.deleteallonsubmit (PTS); <br/> TDC. submitchanges (); </P> <p> foreach (datagridviewrow R in dgv. rows) <br/>{< br/> If (! R. isnewrow) <br/>{< br/> pm_title Pt = new pm_title (); <br/> PT. dataid = datid; <br/> PT. contents = R. cells ["contents"]. value. tostring (); <br/> TDC. pm_title.insertonsubmit (PT); <br/>}< br/> TDC. submitchanges (); </P> <p>}

3. When the data column contains the datagridviewcomboboxcolumn

First, bind the ComboBox data during initialization;

There will be no problem when empty data is added;

When you need to bind data, an exception occurs in the new row "the value of datagridviewcomboboxcolumn is null". In this case, dataerror is added to the datagridview, which can be solved without any processing.

4. When the date Selection control is required in the data column

By default, no date Selection control is available in the column attribute of the datagridview. You can inherit the datetimepicker component in the datagridviewcolumn and C # class libraries to implement your own date Selection control.

Date Selection Component reference: http://blog.csdn.net/lgh112437/archive/2010/01/27/5262122.aspx

PS:

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.