c#

位置:IT落伍者 >> c# >> 浏览文章

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


发布日期:2021年06月01日
 
.net中使用DatagridView的增删改方法

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+) + "行";
}
}
}
执行后的页面效果

               

上一篇:.Net网站的web.config配置说明

下一篇:C#开源资源大汇总