WinForm in DataGridView Display Update data--human version

Source: Internet
Author: User

Using System;  Using System.Collections.Generic;  Using System.ComponentModel;  Using System.Data;  Using system.drawing;  Using System.Linq;  Using System.Text;  Using System.Threading.Tasks;  Using System.Windows.Forms;               
Select a row in the DataGridView, leave, if the data changes are updated to the database, if not changed do not do the operation. Essence: When you click a cell, if you have changed the row and the data changes, it will update the data to the database and load the data from the database. Set the previously clicked cell to the selected state, making the operation experience good. The code is as follows:
1NamespaceUi2{3PublicPartialClassFormstocklist:form4{5PublicFormstocklist ()6{7InitializeComponent ();8This.dgvStockList.AutoGenerateColumns =False//The auto-generated column is set to false;9}1011//Loads the data source for the drop-down list of commodity IDs and DGV data sources.12Privatevoid Formstocklist_load (Objectsender, EventArgs e)13{14//Set the important properties by removing the drop-down list column named Goodsid in the DataGridView.Datagridviewcomboboxcolumn col =this.dgvstocklist.columns["Goodsid"]AsDatagridviewcomboboxcolumn;Col. DataSource =New BLL. GOODSBLL (). Getmodellist ("");Col. DisplayMember ="Name";Col. ValueMember ="Goodsid";Col. Displaystyle =datagridviewcomboboxdisplaystyle.nothing;20Loaddatabypageindex ();//Loads the DataGridView data source.22}2324int cellrowindex;//global variable, storage unit cells number25int cellcolumnindex;//global variable, storage unit column number2627int preselectrowindex;//Stores the line number, which is used later to determine whether the line is wrapped.28BOOL Isfirstselectrow =True//Identifies whether the variable Preselectrowindex has been assigned a value. True if the value is not assigned to Preselectrowindex.29Privatevoid Dgvstocklist_cellclick (Objectsender, DataGridViewCellEventArgs e)30{31//DataGridView DGV = (DataGridView) sender;Cellrowindex =This.dgvStockList.CurrentCell.RowIndex;//Assigning Values to CellrowindexCellcolumnindex =This.dgvStockList.CurrentCell.ColumnIndex;//Assigning Values to Cellcolumnindex34if (Isfirstselectrow)//If Isfirstselectrow is true, indicates that Preselectrowindex is not assigned a value35{Preselectrowindex = E.rowindex;//Assigns the current line number to PreselectrowindexPNS Isfirstselectrow =False//Description has been assigned to Preselectrowindex38}39Else//If you have already assigned a value to Preselectrowindex40{41if (e.rowindex! = Preselectrowindex)//The line number of the currently selected cell is different from the stored line number, which means that the line is wrapped.42{Preselectrowindex = E.rowindex;//The line number where the new row is stored.Selectionrowchanged ();//The method to be executed when the line is swapped. Possible or rebind dgvstocklist data sources.45This.dgvstocklist.rows[cellrowindex]. Cells[cellcolumnindex]. Selected =True//For cases where the dgvstocklist data source may be rebind, reset the currently selected cells to the cell that the user has selected before rebinding the data source.46}47}48This.dgvStockList.BeginEdit (true);//Dgvstocklist_cellclick Click event will select a cell, BeginEdit the current cell can be edited, can trigger the Dgvstocklist_cellbeginedit event.49}5051////Change the row to determine if the value changes, if any, to update the data to the database. Reloads the data source.52PrivatevoidSelectionrowchanged ()53{54if (Isedit)//Determines whether the row has been edited55{56if (temp. Goodsid! = stock. Goodsid | | Temp. Scount! = stock. Scount | | Temp. StockPrice! = stock. StockPrice)//Determines whether the data of the row changes after editing, and if it changes, updates the data to the database.57{58New BLL. STOCKBLL (). Update (stock);//Update the changed row data to the database.Loaddatabypageindex ();//Reloads the Dgvstocklist data source.60}61}Isedit =False//Resets the state of the Isedit.IsFirst =True//Resets the state of the Isfirst.64} Model.stock Stock = null;//The entity object that stores the changed row data. Model.stock temp = null;//The entity object that stores the row data before the change. IsFirst = true;//Identifies whether the row was edited for the first time. isedit bool = false;//Identifies whether a row has been edited. If not, you will not have to update the row data to the database in the future. Dgvstocklist_cellbeginedit private void (object sender, DataGridViewCellCancelEventArgs e). {Isedit = true;//identity The row is edited. "The stock = This.dgvstocklist.selectedrows[0]." Databounditem as model.stock;//the object of the row into the variable Stock (isFirst)//If it is the first time to start editing the line, that line has not been edited. {Model.stock temp = new///store the original data for this row. Goodsid = stock. Goodsid; Temp. Scount = stock. Scount; Temp. StockPrice = stock. StockPrice; Bayi IsFirst = false;//Identifies the row is no longer being edited for the first time. * * * * * * * * * pageIndex int = 1;//Page index--PageCount = 4;//Show several data per page. private void Loaddatabypageindex () this.dg Vstocklist.datasource = new BLL. STOCKBLL (). Getstocklistbypage (PageIndex, PageCount); 89}//Load the dgvstocklist data. 90 91//Click previous Btnpre_click private void (object sender, EventArgs e), 94 pageindex--; (PageIndex < 1) 96 { MeSsagebox.show ("Already on the first page"); pageindex++; Return 98} loaddatabypageindex (); 100}101 102//Click Next 103 private void Btnnext_click (object sender, EventArgs e) 104 {Pagei ndex++;106 if (PageIndex > Model.Save.TPageCount)//model.save.tpagecount The total number of pages saved. 107 {108 MessageBox.Show ("The last page has been "); pageindex--; return;109}110 Loaddatabypageindex (); 111}112}113}

WinForm in DataGridView Display Update data--human version

Related Article

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.