datagrid编辑

前台
=============================================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="datagirdtest.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>datagrid编辑</title>
  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
  <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">
   <h3><font face="宋体">更新数据行</font></h3>
   <span id="Message" EnableViewState="false" runat="server" />
   <p>
    <ASP:DataGrid id="MyDataGrid" runat="server" Width="800px" BackColor="#CCCCFF" BorderColor="Black"
     CellPadding="3" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" DataKeyField="au_id"
     Font-Names="Verdana">
     <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
     <Columns>
      <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
       <ItemStyle Wrap="False"></ItemStyle>
      </asp:EditCommandColumn>
      <asp:TemplateColumn>
       <ItemTemplate>
        <FONT face="宋体">
         <asp:DropDownList id="DropDownList1" runat="server">
          <asp:ListItem Value="1">1</asp:ListItem>
          <asp:ListItem Value="2">2</asp:ListItem>
         </asp:DropDownList></FONT>
       </ItemTemplate>
      </asp:TemplateColumn>
     </Columns>
    </ASP:DataGrid>
  </form>
  </P>
 </body>
</HTML>

后台
===============================

Imports System.Data
Imports System.Data.SqlClient

Public Class WebForm2
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Message As System.Web.UI.HtmlControls.HtmlGenericControl

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region
    Dim conn As SqlConnection
    Dim connstr As String
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码

        '''''''''
        connstr = System.Configuration.ConfigurationSettings.AppSettings("ConnectString")
        conn = New SqlConnection(connstr)
        conn.Open()
        If Not IsPostBack Then
            bindgrid()
        End If


    End Sub
    Function bindgrid()
        Dim myCommand As New SqlDataAdapter("select * from Authors", conn)
        Dim ds As DataSet
        ds = New DataSet
        myCommand.Fill(ds, "Authors")
        MyDataGrid.DataSource = ds.Tables("Authors").DefaultView
        MyDataGrid.DataBind()
    End Function

    Private Sub MyDataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.EditCommand
        MyDataGrid.EditItemIndex = e.Item.ItemIndex
        bindgrid()
    End Sub

    Private Sub MyDataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.UpdateCommand
        ' response.Write ((TextBox)e.Item.Cells[i].Controls[0]).Text;
        Dim t As New TextBox
        t = CType(e.Item.Cells(2).Controls(0), TextBox)
        ' Response.Write(t.Text)
        '读取编辑诓的数据 进行更新 sql
    End Sub

    Private Sub MyDataGrid_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDataGrid.CancelCommand
        MyDataGrid.EditItemIndex = -1
        bindgrid()
    End Sub
End Class

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页