在DataGrid快速添加新行
来源:互联网 发布:周生生淘宝旗舰店假货 编辑:程序博客网 时间:2024/05/17 18:04
asp.net DataGrid为我们提供的内建的记录行编辑功能,但是没有提供内建的添加新行的功能。一个办法就是:在DataTable中添加新行,然后再重新绑定到DataGrid,这个办法可行,但在更新前需要进行确认,可能会产生空行。另外一个解决办法就是:利用DataGrid footer template来提供一个空的行,这样既可以提高速度,也可以避免其它方法带来的不足。为了为浏览者提供一个空行,我们使用DataGrid的Footer Template,我们直接在Footer Template里添加文本框,这样可以避免不必要的操作:比如点击“编辑”按钮等。这样也可以减少往复数据提交的次数。我们这里仍然LinkButton(插入),并设置CommandName属性为“Insert”,这个CommandName在DataGrid的ItemCommand事件中,确保只有用户点击了“Insert”LinkButton才添加记录。添加到数据库的方法是很简单的。 下面的这个例子提供了DataGrid快速添加新行的功能。aspx代码和Cohe Behind代码分别如下,注意更改数据录连接字符串:查看例子InsertableDataGrid.aspx<%@ Page Language="vb" AutoEventWireup="false" Codebehind="InsertableDataGrid.aspx.vb" Inherits="aspxWeb.InserTableDataGrid"%>WebForm1 InsertableDataGrid.aspx.vbImports System.DataImports System.Data.SqlClientPublic Class InserTableDataGrid Inherits System.Web.UI.Page Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. 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 call is 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 SubEnd Class
- 在DataGrid快速添加新行
- 在DataGrid快速添加新行
- 在DataGrid快速添加新行
- 在DataGrid快速添加新行(c#)
- 在DataGrid快速添加新行
- 在DataGrid快速添加新行
- Flex:在有序DataGrid顶部添加新行
- 如何在DataGrid绑定之前为DataSet添加新列
- 如何在DataGrid绑定之前为DataSet添加新列
- 如何在DataGrid绑定之前为DataSet添加新列
- 如何在DataGrid绑定之前为DataSet添加新列
- 如何在DataGrid绑定之前为DataSet添加新列
- 如何在DataGrid绑定之前为DataSet添加新列
- 再议ASP.NET DataGrid控件中的“添加新行”功能
- 再议ASP.NET DataGrid控件中的“添加新行”功能
- 在Datagrid中添加Insert
- [原创]给DataGrid快速添加自动编号
- [原创]给DataGrid快速添加自动编号
- 这18条背下来,没人敢和你忽悠CPU
- 清除“最近使用的文档”
- SQLPLUS命令的使用大全
- 以前用java写的贪吃蛇游戏
- 乘兴而来,兴尽而返
- 在DataGrid快速添加新行
- 一个模拟简单的物品掉落的程序
- 用java写的计算器程序
- RUP之用例间的关系
- 几行代码轻松搞定网页的简繁转换(转载)
- 关掉那可恶的Dos窗口
- 软件版本号详解(转)
- 面试题(杂)
- 快速乘法