在DataGrid快速添加新行

转载 2006年06月15日 14:37:00

下面的这个例子提供了DataGrid快速添加新行的功能。aspx代码和Cohe Behind代码分别如下,注意更改数据录连接字符串:

查看例子

InsertableDataGrid.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="InsertableDataGrid.aspx.vb" Inherits="aspxWeb.InserTableDataGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<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">
<form id="Form1" method="post" runat="server">
  <asp:DataGrid id="DataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None"
   BorderWidth="1px" BackColor="White" CellPadding="4" ShowFooter="True" AutoGenerateColumns="False">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <Columns>
      <asp:TemplateColumn HeaderText="Employee ID">
        <ItemTemplate>
          <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
          </asp:Label>
        </ItemTemplate>
        <FooterTemplate>
          <asp:LinkButton id="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
        </FooterTemplate>
        <EditItemTemplate>
          <asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.employeeid") %>'>
          </asp:TextBox>
        </EditItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="Last Name">
        <ItemTemplate>
          <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.lastname") %>'>
          </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='<%# DataBinder.Eval(Container, "DataItem.firstname") %>'>
          </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" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
  </asp:DataGrid>
</form>
</body>
</HTML>

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 " 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

 

相关文章推荐

在DataGrid中添加新行

  • 2007-11-28 13:14
  • 94KB
  • 下载

Excel VBA 自动添加新行并递增ID

Excel VBA 自动添加新行并递增ID用Excel 2010的VBA功能来处理数据,主要根据IP网段和网段IP个数,来生成每个IP地址,并进行ID编号。Sub AutoInsert() L...
  • wwlhz
  • wwlhz
  • 2017-05-06 18:56
  • 313

使用jquery在table指定行后添加新行

New Document //注意input的id和tr的id要一样 function addRowByID(currentRowID){ //遍历每一行,找...

JQuery实现表格自动增加行,对新行添加事件

效果: 一:原始页面

js在指定的table行后添加新行(js实现select数据的填充,添加行,删除行)

无标题文档         body{width:900px; height:600px;margin: 0px auto; }       body,ul,table,li,pre,dd,d...

C# ListView 在指定行后面插入新行,并对行号重新排序。

在c#中如何用ListView实现在指定行之后插入新的一行,并且不影响原有行的内容,最后对行号重新排序。

sed在指定行插入新行

前些天备份使用mysqldump备份出的数据文件(insert形式),里面的内容没有use db_name这个语句,所以如果在脚本中执行,那么会提示no database selected,所以就想在...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)