Asp.net|datagrid|datagrid control when ASP technology makes Web programming, it is difficult to edit data online because the Recordset object used for the operation of the database does not use a Third-party control. And the DataGrid control supports the function of online editing, as long as the EditCommandColumn property settings appropriate, a little programming can be achieved. The EditItemIndex property of the DataGrid control represents the category of edit buttons, ASP. NET default Edititemindex=-1, that is, editing properties are not supported. Let's learn from the examples below.
Add a Web Form to the Datacon Web project, named Datagrid_sample5.aspx, and add a DataGrid control. The DataGrid control properties are set 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>
<Columns>
<asp:boundcolumn datafield= "id" readonly= "True" headertext= "number" ></asp:BoundColumn>
<asp:boundcolumn datafield= "name" headertext= "name" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Sex" readonly= "True" headertext= "Sex" ></asp:BoundColumn>
<asp:boundcolumn datafield= "class" readonly= "True" headertext= "class" ></asp:BoundColumn>
<asp:boundcolumn datafield= "Address" readonly= "True" headertext= "Home" ></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 instance, we used the template settings for the custom DataGrid control, using the <Columns><asp:BoundColumn> tag when setting the column, where <asp:BoundColumn> The DataField property represents the field name in the bound datasheet, and HeaderText represents the field name that is displayed, so that you can make a friendly datasheet header. When using a custom template, remember to set the value of AutoGenerateColumns to "False", otherwise, in the displayed datasheet will repeat the two sides, one is a custom template form, one is the system according to the data table display. The default is that the bound columns can be edited, but in practice, we often do not need to edit each column's fields, as long as we add readonly= "True" to each <asp:BoundColumn> to mask the editable properties of the column. In this instance, for ease of display, we only require that the "name" field be editable, and that the rest be readonly= "True". There are two types of <asp:EditCommandColumn> ButtonType, namely pushbutton (button style) and LinkButton (hyperlink style), and the system defaults to LinkButton, We use the Bushbutton style in this instance. <asp:buttoncolumn text= "Delete" commandname= "delete" > style is also the top two, we chose the LinkButton style.
Let's look at the Logical Encodings section in DataGrid_Sample5.aspx.vb:
'----Code BEGIN-----
'----omit a reference to a namespace
Public Class Datagrid_sample5
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' Omit the code generated by the form designer 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
' Here 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 ("program error, information described below:<br>" & Ex.) message.tostring)
Finally
Mycon. Close ()
End Try
End Sub
' Paging Event process
Private Sub datagrid1_pageindexchanged (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles datagrid1.pageindexchanged
Datagrid1.currentpageindex = E.newpageindex
GetData ()
End Sub
' Request an arrangement sequence event procedure
Private Sub Datagrid1_sortcommand (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles Datagrid1.sortcommand
ViewState ("sort") = E.sortexpression.tostring
GetData ()
End Sub
' Switch to update status
Private Sub Datagrid1_editcommand (ByVal source as Object, ByVal e as System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles Datagrid1.editcommand
Datagrid1.edititemindex = E.item.itemindex
GetData ()
' Refresh Data
End Sub
' Cancel Edit state
Private Sub Datagrid1_cancelcommand (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Datagrid1.cancelcommand
Datagrid1.edititemindex =-1
' Switch to normal state
GetData ()
' Refresh Data
End Sub
' The Datagrid1_updatecommand event procedure is managed by the. NET Framework runtime, and when the Update button is pressed, the update data process is performed
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 record ID value that gets the current change.
' 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 that gets the current change, CType (E.item.cells (1). Controls (0),
' TextBox). Text is first to E.item.cells (1). The Controls (0) is converted to a TextBox control type,
' and get 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 ("Congratulations!" <script>alert. You have successfully updated the "& name &" Record! ');</script> ")
Datagrid1.edititemindex =-1
' Change Edit button status
GetData ()
' Refresh Data
End Sub
' Delete Logging event procedure
Private Sub Datagrid1_deletecommand (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Datagrid1.deletecommand
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 ("Congratulations!" <script>alert. You have successfully deleted the "& E.item.cells (1). Text & "Record! ');</script> ")
Mycon. Close ()
GetData ()
End Sub
End Class
'---cdoe end----------
In the several sub processes above, the Datagrid1_updatecommand process is a difficult one, in which we use E to get the ID number of the edited record. Item.cells (0). Text.tostring to get, E represents the Incoming object, item represents the data row, the cell represents the cells, and the text represents the value. To get the new value edited, we use CType (E.item.cells (1). Controls (0), TextBox). Text statement, where E. Item.cells (1). Controls (0) represents a cell's control, converts it to a TextBox using CType, and then we can get its text value, which is the updated value. After obtaining the ID number and the updated value of the record, we can write the SQL UPDATE statement for updating the data.
After you save the datagrid_sample5.aspx to compile, the effect is shown in Figure 9.10.
Figure 9.10 Datagrid_sample5.aspx Run Results