.net中使用DatagridView的增刪改方法
來源:易賢網(wǎng) 閱讀:790 次 日期:2014-08-29 10:48:57
溫馨提示:易賢網(wǎng)小編為您整理了“.net中使用DatagridView的增刪改方法”,方便廣大網(wǎng)友查閱!

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://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<!--GridView中必須寫的幾個事件:onrowediting、onrowupdating、onrowcancelingedit、onrowdeleting--->

<asp:GridView ID="GridView1" 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 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="Label2" 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="Label3" runat="server" Text='<%# Eval("TextName") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:CommandField HeaderText="狀態(tài)" 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>

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

{ //綁定數(shù)據(jù)源

GridView1.DataSource = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0];

GridView1.DataBind();

}

catch (Exception ex){}

}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

{

GridView1.PageIndex = e.NewPageIndex;

GridViewBind();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

GridViewBind();

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{ //gridview編輯項索引等于單擊行的索引

GridView1.EditIndex = e.NewEditIndex;

GridViewBind();

}

/// <summary>

/// 修改事件

/// </summary>

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{ //取出編號

int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());

//獲取修改的值的內(nèi)容

string TableName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTableName")).Text;

string TextName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTextName")).Text;

//更新記錄

string sql = string.Format("update dbo.GroupType_Demo set TableName='{0}',TextName='{1}' where Num={2}",TableName,TextName,Num);

try

{

int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);

if (i > 0)

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('修改成功!')</script>");

}

else

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('修改失敗!')</script>");

}

}

catch (Exception ex)

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('操作無效!')</script>");

}

GridView1.EditIndex = -1;

GridViewBind();

}

/// <summary>

/// 刪除事件

/// </summary>

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

//取出編號

int Num = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblNum")).Text.ToString());

//更新記錄

string sql = string.Format("delete dbo.GroupType_Demo where num={0}", Num);

try

{

int i = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql);

if (i > 0)

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('刪除成功!')</script>");

}

else

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('刪除失敗!')</script>");

}

}

catch (Exception ex)

{

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script language='javascript'>alert('操作無效!')</script>");

}

GridView1.EditIndex = -1;

GridViewBind();

}

/// <summary>

/// 添加按鈕

/// </summary>

protected void btnAdd_Click(object sender, EventArgs e)

{

Response.Redirect("~/Default_Add.aspx");

}

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)

{

//行號

int i = e.NewSelectedIndex;

GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

btnAdd.Text = "你選中了第" + (i+1) + "行。";

}

}

}

執(zhí)行后的頁面效果:

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:.net中使用DatagridView的增刪改方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)