45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:怎么样在DataGrid快速添加新行?

怎么样在DataGrid快速添加新行?

2016-08-25 15:29:21 来源:www.45fan.com 【

怎么样在DataGrid快速添加新行?

在DataGrid快速添加新行

http://lucky_elove.www1.dotnetplayground.com/

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

<%@ autoeventwireup="false" codebehind="InsertableDataGrid.aspx.vb" inherits="aspxWeb.InserTableDataGrid" language="vb" page=""> <!doctype 4.0="" dtd="" html="" public=""> <html> <head> <title></title> <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR" /> <meta content="Visual Basic 7.0" name="CODE_LANGUAGE" /> <meta content="JavaScript" name="vs_defaultClientScript" /> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema" /> </head> <body ms_positioning="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:datagrid autogeneratecolumns="False" backcolor="White" bordercolor="#CC9966" borderstyle="None" borderwidth="1px" cellpadding="4" id="DataGrid1" runat="server" showfooter="True"> <selecteditemstyle backcolor="#FFCC66" font-bold="True" forecolor="#663399"></selecteditemstyle> <itemstyle backcolor="White" forecolor="#330099"></itemstyle> <headerstyle backcolor="#990000" font-bold="True" forecolor="#FFFFCC"></headerstyle> <footerstyle backcolor="#FFFFCC" forecolor="#330099"></footerstyle> <columns> <asp:templatecolumn headertext="Employee ID"> <itemtemplate> <asp:label id="Label3" runat="server" text="&lt;%# DataBinder.Eval(Container, &quot;DataItem.employeeid&quot;) %&gt;"> </asp:label> </itemtemplate> <footertemplate> <asp:linkbutton commandname="Insert" id="LinkButton1" runat="server">Insert</asp:linkbutton> </footertemplate> <edititemtemplate> <asp:textbox id="TextBox5" runat="server" text="&lt;%# DataBinder.Eval(Container, &quot;DataItem.employeeid&quot;) %&gt;"> </asp:textbox> </edititemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="Last Name"> <itemtemplate> <asp:label id="Label1" runat="server" text="&lt;%# DataBinder.Eval(Container, &quot;DataItem.lastname&quot;) %&gt;"> </asp:label> </itemtemplate> <footertemplate> <asp:textbox id="TextBox2" runat="server"></asp:textbox> </footertemplate> <edititemtemplate> <asp:textbox id="TextBox1" runat="server"></asp:textbox> </edititemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="First Name"> <itemtemplate> <asp:label id="Label2" runat="server" text="&lt;%# DataBinder.Eval(Container, &quot;DataItem.firstname&quot;) %&gt;"> </asp:label> </itemtemplate> <footertemplate> <asp:textbox id="TextBox4" runat="server"></asp:textbox> </footertemplate> <edititemtemplate> <asp:textbox id="TextBox3" runat="server"></asp:textbox> </edititemtemplate> </asp:templatecolumn> </columns> <pagerstyle backcolor="#FFFFCC" forecolor="#330099" horizontalalign="Center"></pagerstyle> </asp:datagrid></form> </body> </html> </!doctype></%@><%@ page="" language="vb" autoeventwireup="false" codebehind="InsertableDataGrid.aspx.vb" inherits="aspxWeb.InserTableDataGrid">

InsertableDataGrid.aspx.vb

Imports System.Data Imports System.Data.SqlClient Public Class InserTableDataGrid Inherits System.Web.UI.Page Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid #Region &quot; Web Form Designer Generated Code &quot; &#39;This call is 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 &#39;CODEGEN: This method call is required by the Web Form Designer &#39;Do not modify it using the code editor. InitializeComponent() End Sub #End Region Dim connstr As String = &quot;Integrated Security=SSPI;User ID=sa;Initial Catalog=NorthWind;Data Source=./netsdk&quot; 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(&quot;select employeeid,lastname,firstname from employees&quot;, cnn) Dim ds As New DataSet() da.Fill(ds, &quot;employees&quot;) 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 = &quot;Insert&quot; Then Dim cnn As New SqlConnection(connstr) Dim t1 As TextBox = e.Item.FindControl(&quot;textbox2&quot;) Dim t2 As TextBox = e.Item.FindControl(&quot;textbox4&quot;) cnn.Open() Dim cmd As New SqlCommand(&quot;insert into employees(lastname,firstname) values(&#39;&quot; &amp; t1.Text &amp; &quot;&#39;,&#39;&quot; &amp; t2.Text &amp; &quot;&#39;)&quot;, cnn) cmd.ExecuteNonQuery() cnn.Close() BindGrid() End If End Sub End Class </system.diagnostics.debuggerstepthrough()>

本文地址:http://www.45fan.com/dnjc/67466.html
Tags: 快速 DataGrid 新行
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部