asp.net中使用DatagridView的增删改方法

2013 年 2 月 17 日4960

欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入

  default.aspx 页面:

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GPS_Web.Default" %>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://http://www.zjjv.com///TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <html xmlns="http://http://www.zjjv.com///1999/xhtml" >

  <head runat="server">

  <title></title>

  </head>

  <body>

  <form runat="server">

  <div>

  <!--GridView中必须写的几个事件:onrowediting、onrowupdating、onrowcancelingedit、onrowdeleting--->

  <asp:GridView runat="server" AutoGenerateColumns="False"

  onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit"

  onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"

  onrowdeleting="GridView1_RowDeleting"

  onselectedindexchanging="GridView1_SelectedIndexChanging">

  <Columns>

  <asp:TemplateField HeaderText="编号" Visible="False">

  <ItemTemplate>

  <asp:Label runat="server" Text='<%# Eval("Num") %>'></asp:Label>

  </ItemTemplate>

  </asp:TemplateField>

  <asp:TemplateField HeaderText="姓名">

  <EditItemTemplate>

  <asp:TextBox runat="server" Text='<%# Eval("TableName") %>'></asp:TextBox>

  </EditItemTemplate>

  <ItemTemplate>

  <asp:Label runat="server" Text='<%# Eval("TableName") %>'></asp:Label>

  </ItemTemplate>

  </asp:TemplateField>

  <asp:TemplateField HeaderText="地址">

  <EditItemTemplate>

  <asp:TextBox runat="server" Text='<%# Eval("TextName") %>'></asp:TextBox>

  </EditItemTemplate>

  <ItemTemplate>

  <asp:Label runat="server" Text='<%# Eval("TextName") %>'></asp:Label>

  </ItemTemplate>

  </asp:TemplateField>

  <asp:CommandField HeaderText="状态" ShowSelectButton="True" />

  <asp:CommandField HeaderText="编辑" ShowEditButton="True" />

  <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />

  </Columns>

  </asp:GridView>

  <br />

  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  <asp:Button runat="server" Text="添加" />

  <br />

  <br />

  </div>

  </form>

  </body>

  </html>

  default.aspx.cs页面代码:

  using System;

  using System.Web.UI;

  using System.Web.UI.WebControls;

  using GPS_Web.ywpages.DAL;

  using System.Data;

  namespace GPS_Web

  {

  /// <summary>

  ///

  /// </summary>

  public partial class Default : System.Web.UI.Page

  {

  protected void Page_Load(object sender, EventArgs e)

  {

  if (!Page.IsPostBack)

  {

  GridViewBind();

  }

  }

  private void GridViewBind()

  {

  string sql = "select Num,TableName,TextName from dbo.GroupType_Demo";

  try

[1][2]下一页

【责编:peter】

0 0