Switch from it168: DataGrid control editing, update, and delete Functions

Source: Internet
Author: User
When ASP is used for Web programming, it is difficult to edit data online without using third-party controls because the recordset object used for database operations. The DataGrid Control supports the online editing function. You only need to set the editcommandcolumn attribute appropriately and implement it with a little programming. The edititemindex attribute of the DataGrid Control indicates the category of the edit button. The default edititemindex =-1 in ASP. NET does not support editing attributes. Next we will learn through examples.

(Related articles:ASP. NET: getting started with database binding controls)
(Related articles:ASP. NET: databind Method for database binding controls)
(Related articles:ASP. NET: The page function of the DataGrid Control)
(Related articles:ASP. NET: Sorting function of the DataGrid Control)

In the datacon web project, add a web form named datagrid_sample5.aspx and add a DataGrid Control. The DataGrid Control attribute settings are as follows:

<Asp: DataGrid id = "datagrid1"
Runat = "server" autogeneratecolumns = "false"
Height = "282px" allowpaging = "true">
<Alternatingitemstyle font-size = "X-small" backcolor = "gainsboro"> </alternatingitemstyle>
<Itemstyle font-size = "X-small" backcolor = "whitesmoke"> </itemstyle>
<Headerstyle backcolor = "#99 CCCC"> <Columns>
<Asp: boundcolumn datafield = "ID" readonly = "true" headertext = "no"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "name" headertext = "name"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "sex" readonly = "true" headertext = "gender"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "class" readonly = "true" headertext = "class"> </ASP: boundcolumn>
<Asp: boundcolumn datafield = "Address" readonly = "true" headertext = "Address"> </ASP: boundcolumn>
<Asp: editcommandcolumn buttontype = "Pushbutton" updatetext = "Update" headertext = "edit" canceltext = "cancel" edittext = "edit"> </ASP: editcommandcolumn>
<Asp: buttoncolumn text = "delete" commandname = "delete" headertext = "delete"> </ASP: buttoncolumn>
</Columns>
<Pagerstyle position = "topandbottom" mode = "numericpages"> </pagerstyle>
</ASP: DataGrid>

In this example, we use the template settings of the custom DataGrid Control, and use the <columns> <asp: boundcolumn> flag when setting columns, where <asp: the datafield attribute of boundcolumn> indicates the name of the field in the bound data table, while headertext indicates the name of the field displayed. In this way, a friendly data table header can be created. When using a custom template, remember to set the autogeneratecolumns value to "false". Otherwise, the two sides are repeatedly displayed in the displayed data table, in the form of a custom template, one is displayed by the system according to the data table. By default, the bound columns can be edited. However, in actual applications, you do not need to edit the fields in each column: add readonly = "true" to boundcolumn> to block the Editable attribute of this column. In this instance, only the "name" field is required to be editable for ease of display, and the others are readonly = "true ". <Asp: editcommandcolumn> supports two types of buttontypes: Pushbutton and linkbutton. The default value is linkbutton, we use the bushbutton style in this instance. <Asp: buttoncolumn text = "delete" commandname = "delete"> the above two styles are selected.

Let's take a look at the logic encoding section in datagrid_sample5.aspx.vb:
'---- Code begin -----
'---- Omitting the namespace reference
Public class datagrid_sample5
Inherits system. Web. UI. Page
# Region "code generated by web forms designer"
'The code generated by the Form Designer is omitted here.
# End Region
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
If not ispostback then
Getdata ()
'Call the data binding process
End if
End sub
'Below is the data binding process
Sub getdata ()
Dim mycon as oledb. oledbconnection
Try
Mycon = new oledb. oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (".") + "\ studentinfor. mdb ")
Dim mycmd as oledb. oledbdataadapter = new oledb. oledbdataadapter ("select ID, name, sex, class, address from student", mycon)
Dim dT as data. dataset = new data. Dataset
Mycmd. Fill (DT)
Datagrid1.datasource = DT. Tables (0). defaultview
Datagrid1.databind ()
Catch ex as exception
Response. Write ("the program has an error. The information is described as follows: <br>" & Ex. Message. tostring)
Finally
Mycon. Close ()
End try
End sub
'Page flip event Process
Private sub maid (byval source as object, byval e as system. Web. UI. webcontrols. datagridpagechangedeventargs) handles maid
Datagrid1.currentpageindex = E. newpageindex
Getdata ()
End sub
'Request ordered event Process
Private sub maid (byval source as object, byval e as system. Web. UI. webcontrols. datagridsortcommandeventargs) handles maid command
Viewstate ("sort") = E. sortexpression. tostring
Getdata ()
End sub
'Switch to the Update Status
Private sub maid editcommand (byval source as object, byval e as system. Web. UI. webcontrols. datagridcommandeventargs) handles maid command
Datagrid1.edititemindex = E. Item. itemindex
Getdata ()
'Refresh data
End sub
'Cancel editing status
Private sub maid cancelcommand (byval source as object, byval e as system. Web. UI. webcontrols. datagridcommandeventargs) handles maid command
Datagrid1.edititemindex =-1
'Switch to normal
Getdata ()
'Refresh data
End sub
The 'dated1 _ updatecommand event process is hosted when the. NET Framework is running. When the update button is pressed, the data update process is executed.
Private sub datagrid1_updatecommand (byval source as object, byval e as system. Web. UI. webcontrols. datagridcommandeventargs) handles datagrid1.updatecommand
Dim ID as string = E. Item. cells (0). Text. tostring
'Here is the ID value of the current change record,
'E. item. cells (0). Text returns the value of the first column in The DataGrid table ()
Dim name as string = ctype (E. Item. cells (1). Controls (0), textbox). Text
'Here is the value after the current change, ctype (E. Item. cells (1). Controls (0 ),
'Textbox). Text first converts E. Item. cells (1). Controls (0) to the Textbox Control type,
'Then obtain its text value ()
Dim SQL as string
SQL = "Update student set name = '" + name + "'where id =" + id
'Response. Write (SQL)
'Exit sub
Dim constr as string = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (".") + "\ studentinfor. mdb"
Dim mycon as oledb. oledbconnection = new oledb. oledbconnection (constr)
Mycon. open ()
Dim mycmd as oledb. oledbcommand = new oledb. oledbcommand (SQL, mycon)
Mycmd. executenonquery ()
Mycon. Close ()
Response. Write ("<SCRIPT> alert ('Congratulations! You have successfully updated the "& name &" record! '); </SCRIPT> ")
Datagrid1.edititemindex =-1
'Change the editing button status
Getdata ()
'Refresh data
End sub
'Process of deleting record events
Private sub maid (byval source as object, byval e as system. Web. UI. webcontrols. datagridcommandeventargs) handles maid
Dim mycon as oledb. oledbconnection = new oledb. oledbconnection ("provider = Microsoft. jet. oledb.4.0; Data Source = "+ server. mappath (". ") +" \ studentinfor. mdb ")
Dim MYSQL as string
MySQL = "delete from student where id =" & E. Item. cells (0). Text
Mycon. open ()
Dim mycmd as oledb. oledbcommand = new oledb. oledbcommand (MySQL, mycon)
Mycmd. executenonquery ()
Response. Write ("<SCRIPT> alert ('Congratulations! You have successfully deleted the "& E. Item. cells (1). Text &" record! '); </SCRIPT> ")
Mycon. Close ()
Getdata ()
End sub
End Class
'--- Cdoe END ----------

In the previous sub processes, the datagrid1_updatecommand process is difficult. In order to obtain the ID number of the edit record, we use E. item. cells (0 ). text. tostring. e indicates the input object, item indicates the data row, cell indicates the cell, and text indicates the value. To obtain the new value after editing, we use ctype (E. item. cells (1 ). controls (0), textbox ). text statement, where E. item. cells (1 ). controls (0) indicates the control of the cell. Use ctype to convert it to Textbox, and then we can get its text value, which is the updated value. After obtaining the record ID and updated value, we can write an SQL update statement to update the data.

Save and compile the datagrid_sample5.aspx file, and the running effect is 9.10.


Figure 9.10 running result of datagrid_sample5.aspx

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.