熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.net中使用DatagridView的增刪改方法

2022-06-13   來源: .NET編程 

  defaultaspx 頁面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Defaultaspxcs" Inherits="GPS_WebDefault" %>
<!DOCTYPE html PUBLIC "//WC//DTD XHTML Transitional//EN" "
<html xmlns="
<head runat="server">
<title></title>
</head>
<body>
<form id="form" runat="server">
<div>
<!GridView中必須寫的幾個事件onroweditingonrowupdatingonrowcancelingeditonrowdeleting>
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False"
onpageindexchanging="GridView_PageIndexChanging" onrowcancelingedit="GridView_RowCancelingEdit"
onrowediting="GridView_RowEditing" onrowupdating="GridView_RowUpdating"
onrowdeleting="GridView_RowDeleting"
onselectedindexchanging="GridView_SelectedIndexChanging">
<Columns>
<asp:TemplateField HeaderText="編號" Visible="False">
<ItemTemplate>
<asp:Label ID="lblNum" runat="server" Text=<%# Eval("Num") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名">
<EditItemTemplate>
<asp:TextBox ID="txtTableName" runat="server" Text=<%# Eval("TableName") %>></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label" runat="server" Text=<%# Eval("TableName") %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="地址">
<EditItemTemplate>
<asp:TextBox ID="txtTextName" runat="server" Text=<%# Eval("TextName") %>></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="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 />
 
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="添加" />
<br />
<br />
</div>
</form>
</body>
</html>
defaultaspxcs頁面代碼
using System;
using SystemWebUI;
using SystemWebUIWebControls;
using GPS_WebywpagesDAL;
using SystemData;
namespace GPS_Web
{
/// <summary>
/// </summary>
public partial class Default : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
if (!PageIsPostBack)
{
GridViewBind();
}
}
private void GridViewBind()
{
string sql = "select NumTableNameTextName from dboGroupType_Demo";
try
{ //綁定數據源
GridViewDataSource = SqlHelperExecuteDataset(SqlHelperGetConnSting() CommandTypeText sql)Tables[];
GridViewDataBind();
}
catch (Exception ex){}
}
protected void GridView_PageIndexChanging(object sender GridViewPageEventArgs e)
{
GridViewPageIndex = eNewPageIndex;
GridViewBind();
}
protected void GridView_RowCancelingEdit(object sender GridViewCancelEditEventArgs e)
{
GridViewEditIndex = ;
GridViewBind();
}
protected void GridView_RowEditing(object sender GridViewEditEventArgs e)
{ //gridview編輯項索引等於單擊行的索引
GridViewEditIndex = eNewEditIndex;
GridViewBind();
}
/// <summary>
/// 修改事件
/// </summary>
protected void GridView_RowUpdating(object sender GridViewUpdateEventArgs e)
{ //取出編號
int Num = ConvertToInt(((Label)GridViewRows[eRowIndex]FindControl("lblNum"))TextToString());
//獲取修改的值的內容
string TableName = ((TextBox)GridViewRows[eRowIndex]FindControl("txtTableName"))Text;
string TextName = ((TextBox)GridViewRows[eRowIndex]FindControl("txtTextName"))Text;
//更新記錄
string sql = stringFormat("update dboGroupType_Demo set TableName={}TextName={} where Num={}"TableNameTextNameNum);
try
{
int i = SqlHelperExecuteNonQuery(SqlHelperGetConnSting() CommandTypeText sql);
if (i > )
{
ClientScriptRegisterStartupScript(ClientScriptGetType() "myscript" "<script language=javascript>alert(修改成功!)</script>");
}
else
{
ClientScriptRegisterStartupScript(ClientScriptGetType() "myscript" "<script language=javascript>alert(修改失敗!)</script>");
}
}
catch (Exception ex)
{
ClientScriptRegisterStartupScript(ClientScriptGetType() "myscript" "<script language=javascript>alert(操作無效!)</script>");
}
GridViewEditIndex = ;
GridViewBind();
}
/// <summary>
/// 刪除事件
/// </summary>
protected void GridView_RowDeleting(object sender GridViewDeleteEventArgs e)
{
//取出編號
int Num = ConvertToInt(((Label)GridViewRows[eRowIndex]FindControl("lblNum"))TextToString());
//更新記錄
string sql = stringFormat("delete dboGroupType_Demo where num={}" Num);
try
{
int i = SqlHelperExecuteNonQuery(SqlHelperGetConnSting() CommandTypeText sql);
if (i > )
{
ClientScriptRegisterStartupScript(ClientScriptGetType() "myscript" "<script language=javascript>alert(刪除成功!)</script>");
}
else
{
ClientScriptRegisterStartupScript(ClientScriptGetType() "myscript" "<script language=javascript>alert(刪除失敗!)</script>");
}
}
catch (Exception ex)
{
ClientScriptRegisterStartupScript(ClientScriptGetType() "myscript" "<script language=javascript>alert(操作無效!)</script>");
}
GridViewEditIndex = ;
GridViewBind();
}
/// <summary>
/// 添加按鈕
/// </summary>
protected void btnAdd_Click(object sender EventArgs e)
{
ResponseRedirect("~/Default_Addaspx");
}
protected void GridView_SelectedIndexChanging(object sender GridViewSelectEventArgs e)
{
//行號
int i = eNewSelectedIndex;
GridViewRow row = GridViewRows[eNewSelectedIndex];

btnAddText = "你選中了第" + (i+) + "行";
}
}
}
執行後的頁面效果

  


From:http://tw.wingwit.com/Article/program/net/201311/14171.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.