The Asp.net|datagrid asp.net datagrid provides us with the built-in record line editing functionality, but does not provide built-in functionality to add new rows. One way to do this is to add a new row to the DataTable, and then rebind it to the DataGrid, which works, but it needs to be validated before it is updated, and a blank line may be generated. Another solution is to use the DataGrid footer template to provide an empty line, which can either increase speed or avoid the drawbacks of other methods.
To provide a blank line for the viewer, we use the footer Template of the DataGrid, and we add a text box directly to footer Template to avoid unnecessary actions such as clicking on the Edit button. This can also reduce the number of reciprocating data submissions. We still LinkButton (insert) and set the CommandName property to "Insert", this commandname in the ItemCommand event of the DataGrid to ensure that only the user clicked "Insert" LinkButton only add records. The method to add to the database is simple.
The following example provides the ability for the DataGrid to quickly add new rows. The ASPX code and the Cohe behind code are as follows, noting that changing the data recording connection string:
Public Class Insertabledatagrid
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 as System.Web.UI.WebControls.DataGrid
#Region "Web Form Designer generated Code"
' This are required by the Web Form Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
End Sub
Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mybase.init
' Codegen:this Method-required by the Web Form Designer
' Do not modify it using the ' Code Editor.
InitializeComponent ()
End Sub
#End Region
Dim connstr as String = "Integrated Security=sspi;" User id=sa;initial catalog=northwind;data source=.\netsdk "
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
If not Page.IsPostBack Then
Bindgrid ()
End If
End Sub
Sub Bindgrid ()
Dim CNN as New SqlConnection (CONNSTR)
Dim da as New SqlDataAdapter ("Select Employeeid,lastname,firstname from Employees", CNN)
Dim DS as New DataSet ()
Da. Fill (ds, "Employees")
DataGrid1.DataSource = ds
Datagrid1.databind ()
End Sub
Private Sub Datagrid1_itemcommand (ByVal source as Object, ByVal e as System.Web.UI.WebControls.DataGridCommandEventArgs )_
Handles Datagrid1.itemcommand
If e.commandname = "Insert" Then
Dim CNN as New SqlConnection (CONNSTR)
Dim T1 as TextBox = E.item.findcontrol ("TextBox2")
Dim T2 as TextBox = E.item.findcontrol ("TextBox4")
Cnn. Open ()
Dim cmd as New SqlCommand (insert INTO employees (Lastname,firstname) VALUES (' "& T1. Text & "', '" & T2. Text & "')", CNN)
Cmd. ExecuteNonQuery ()
Cnn. Close ()
Bindgrid ()
End If
End Sub
End Class
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.