在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

<xmp><%@ Page Language="vb" AutoEventWireup="false" Codebehind="InsertableDataGrid.aspx.vb" Inherits="aspxWeb.InserTableDataGrid"%> <html> <head> WebForm1 <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body ms_positioning="GridLayout"> <asp:datagrid showfooter="True" borderwidth="1px" bordercolor="#CC9966" autogeneratecolumns="False" id="DataGrid1" borderstyle="None" backcolor="White" cellpadding="4" runat="server"> <selecteditemstyle font-bold="True" backcolor="#FFCC66" forecolor="#663399"></selecteditemstyle> <itemstyle backcolor="White" forecolor="#330099"></itemstyle> <headerstyle font-bold="True" backcolor="#990000" forecolor="#FFFFCC"></headerstyle> <footerstyle backcolor="#FFFFCC" forecolor="#330099"></footerstyle> <columns> <asp:templatecolumn headertext="Employee ID"> <itemtemplate> <asp:label text="&lt;%# DataBinder.Eval(Container, " dataitem.employeeid")="" %="">" id="Label3" runat="server"> </asp:label> </itemtemplate> <footertemplate> <asp:linkbutton commandname="Insert" id="LinkButton1" runat="server">Insert</asp:linkbutton> </footertemplate> <edititemtemplate> <asp:textbox text="&lt;%# DataBinder.Eval(Container, " dataitem.employeeid")="" %="">" id="TextBox5" runat="server"> </asp:textbox> </edititemtemplate> </asp:templatecolumn> <asp:templatecolumn headertext="Last Name"> <itemtemplate> <asp:label text="&lt;%# DataBinder.Eval(Container, " dataitem.lastname")="" %="">" id="Label1" runat="server"> </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 text="&lt;%# DataBinder.Eval(Container, " dataitem.firstname")="" %="">" id="Label2" runat="server"> </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 horizontalalign="Center" backcolor="#FFFFCC" forecolor="#330099"></pagerstyle> </asp:datagrid> </body> </html> </xmp>

InsertableDataGrid.aspx.vb

<xmp>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 " Web Form Designer Generated Code " '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 '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 Sub End Class </xmp>
在DataGrid中添加新行 立即下载

相关推荐

从光斑图到锤形优化:一次搞定Zemax双胶合透镜的像散与场曲问题

本文详细解析了Zemax双胶合透镜设计中像散与场曲问题的诊断与优化方法。从光斑图分析到锤形优化技巧,提供了完整的光学设计实战指南,帮助设计师快速定位问题并有效改善系统性能,特别适合光学设计工程师和Zemax使用者参考。

weixin_27134495的博客 235

datagrid系列之-基本添加行,删除行,插入行,自定义方法

一、需要引入的文件 <link href="${ctxStatic}/easyui/css/easyui.css" rel="stylesheet" /> <link href="${ctxStatic}/easyui/css/icon.css" rel="stylesheet" /> <script src="${ctxStatic}/easyui/js/jqu...

weinichendian的博客 1万+

2026年AI技能进阶指南:从提示词工程到智能体开发实战

提示词工程作为AI时代的基础沟通技能,其核心原理是通过系统化的交互方法论实现与大模型的高效对话。这项技术通过明确性、具体性和可操作性的设计原则,显著提升了AI生成内容的质量和实用性。在工程实践层面,提示词工程已经发展出角色扮演、步骤分解等成熟模式,广泛应用于代码生成、技术方案设计等场景。随着AI智能体开发的兴起,提示词工程进一步演化为定义智能体身份认知和行为规范的系统接口设计,成为构建自主AI系统的关键技术基础。掌握从基础提示词到智能体开发的完整技能栈,能够帮助开发者在AI技术浪潮中保持核心竞争力。

weixin_33139275的博客 335

c# DataGridView动态添加新行的二个方法

 方法1:常用的方法   复制代码 代码示例:int index=this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = "1"; this.dataGridView1.Rows[index].Cells[1].Value = "2"; this.dataGridView1.Row

yuanyuan_186的专栏 4418

Tesseract OCR 字库训练教程:提高识别率进阶版

通过 Tesseract 字库训练,你可以提高 OCR 对特定文本或字体的识别能力。训练过程包括准备训练数据、生成标签文件、运行训练工具,以及将训练结果集成到实际项目中。虽然训练过程需要一些时间和精力,但对于需要高精度文本识别的场景来说,字库训练是非常有效的提升识别率的方法。

小蜗牛的珍贵百宝箱 1874

计算机组成原理实验:手把手教你用模拟程序实现中断机制(附完整代码)

本文通过一个计算机组成原理模拟实验,手把手教你从零实现一个可工作的中断系统。实验详细演示了如何配置中断控制器、编写主程序与中断服务程序,并观察中断响应、现场保护和恢复的全过程,深入解析了中断原理的核心机制。

474

easyui-datagrid删除行,添加行

这里我遇到的问题是怎么点击分配按钮把左边表单中选中的内容删除,并且在右边的表单中添加选中的那些内容。 经过查阅百度及easyui的官方文档,我解决用到了下面的几个datagrid中的方法: 用到的方法: getSelections : 返回所有选中的行,当没有选中的记录时,将返回空数组。 getRowIndex : 返回指定行的索引,row 参数可以是一个行记录或者一个 id 字段的值

a4638966的博客 2万+

从零搭建VGGNet

在 forward 方法中实现了前向传播过程,包括卷积、激活、池化和平铺操作,最后通过全连接层得到分类结果。使用 torch.onnx.export 将模型导出为 ONNX 格式的文件 vgg.onnx。导出 ONNX:将模型导出为 ONNX 格式,便于在其他平台或框架中使用。定义 VGG 网络:构建了一个包含多个卷积层、池化层和全连接层的网络。使用 torch.save 将整个模型保存为 vgg.pth 文件。前向传播:对输入数据进行前向传播,得到输出结果。保存模型:将模型保存为 .pth 文件。

开源驱动,创造价值;坦诚清晰,快速失败;让复杂可计算。 522

C# DataGridView控件 动态添加新行

C# DataGridView控件 动态添加新行 DataGridView控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为DataGridView控件添加相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方法: 方法一: int index...

dilingyin5446的博客 346

15、人工智能社会学:关系视角下的机器相遇

本文探讨了将人工智能视为社会主体的可能性,提出了一种关系视角下的人工智能社会学。通过结合卢西亚娜·帕里西、艾伦·图灵和乔治·赫伯特·米德的理论,强调不确定性在人工智能运行中的核心地位,并分析了人工智能如何通过与社会环境的互动催生新奇性,从而拓展社会学的研究范畴。文章还讨论了人工智能对社会结构和社会互动的影响,以及其作为社会主体所带来的理论和实践意义,为未来人工智能社会学的发展提供了方向。

pea55的博客 74

全网最强AI辅助课题申报教程!超详细流程拆解,助你快速提高课题申报成功率(附各部分高效提示词)

实验法要详细说明自变量、因变量、实验类型、实验过程、实验步骤、测量工具名称、被试取样方式、无关变量控制方法。这个部分就是让评审专家相信我们有能力把这个项目干成,写的时候要重点突出两块内容:第一是研究基础,包括你或团队在相关领域已有的研究成果,比如发表的论文、承担过的课题、获得过的奖励、相关研究经验等,要具体、有说服力;课题申报专门给我们留了一小节用来突出课题的创新之处,不管是研究角度、研究对象、研究方法,还是应用价值都可以提炼作为我们的创新点,最后的语气成果也要明确具体,且要和课题内容紧密相关。

智能AI让学术科研更容易 667

在DataGrid快速添加新行 (转)

在DataGrid快速添加新行 (转)[@more@]在DataGrid快速添加新行 do.NETplayground.com/">http://lucky_elove.www1.dotnetplayground.com...

ciya3282的博客 161

如何用Python打造PubMed API客户端:科研文献检索自动化实践

PubMed提供了官方的Entrez E-utilities API接口,支持程序化检索文献,但直接调用API需处理请求重试、参数封装、数据解析等问题。批量获取文献ID及结构化详情(标题、作者、DOI等)内置重试机制,应对网络波动或API限流统一数据输出格式,便于后续分析(如文献计量、AI文本处理)灵活适配不同检索需求(如指定数据库、结果数量)本文实现的PubMed API客户端,通过封装API请求、重试机制、数据解析,让科研文献检索从“手动点击”变为“代码自动化”。

ASKCOS博客 360

在DataGrid快速添加新行(c#)

appe_admin.aspx <%@ Page language="c#" Codebehind="appe_admin.aspx.cs" AutoEventWireup="false" Inherits="bzh_home.appe_admin" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ...

weixin_34194087的博客 133

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

在Web中的Datagrid中添加一行

在DataGrid快速添加新行 ASP.NET DataGrid为我们提供的内建的记录行编辑功能,但是没有提供内建的添加新行的功能。一个办法就是:在DataTable中添加新行,然后再重新绑定到DataGrid,这个办法可行,但在更新前需要进行确认,可能会产生空行。另外一个解决办法就是:利用DataGrid footer template来提供一个空的行,这样既可以提高速度,也可以避免其它方法带...

weixin_30297281的博客 346

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

【分享】WebForm中DataGrid的20篇经典文章

1、DataGrid动态模板列更新数据并且分页的例子http://www.cnblogs.com/lovecherry/archive/2005/03/26/126102.html2、DataGrid动态添加模板列的一个例子http://www.cnblogs.com/lovecherry/archive/2005/03/25/125526.html3、DataGrid实现增删(带提示)改和分页 ...

weixin_30681615的博客 394

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

关于DataGrid的精华

document.title="【转贴优快云】关于DataGrid的精华 - "+document.title【转贴优快云】关于DataGrid的精华document.title="【转贴优快云】关于DataGrid的精华 - "+document.title1、DataGrid动态模板列更新数据并且分页的例子 http://www.cnblogs.com/

干一行,爱一行,钻一行 688

再议ASP.NET DataGrid控件中的“添加新行”功能

在园子里搜索了一下,发现不管是panchengyong的“在DataGrid快速添加新行(c#) ”还是piccolo的”在DataGrid快速添加新行“,都只是简单的实现了添加新行,功能上还是有改进的余地。说起在DataGrid中添加新行,ASP.NET开发的大牛人物:Dino Esposito,他的《构建WEB解决方案--应用ASP.NET和ADO.NET》一书中提供了和以上两位类似的方法,但

刚刚.Net相关知识技术互动平台(粉色回忆) 1040
上一篇: 把Excel文件中的数据读入到DataGrid中
下一篇: 一个可逆加密的例子
java2000_test
博客等级 码龄19年 10粉丝 320原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值