使用gridview插入编辑删除 [英] insert edit delete by using gridview
问题描述
晚上好,
i我正在插入,使用gridview编辑删除。
我的aspx代码ID
<%@ Page 语言 = C# AutoEventWireup = true CodeFile = GridView2.aspx.cs 继承 = GridView2 %>
< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN < span class =code-attribute>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < / title >
< / head >
< body >
< 表格 < span class =code-attribute> id = form1 runat = 服务器 >
< div >
< asp:按钮 ID = btnadd runat = server 文字 = 添加 onclick = btnadd_Click / >
< asp:GridView ID = gv runat = server AutoGenerateColumns = false
onrowdeleting = gv_RowDeleting onrowediting = gv_RowEditing
onrowupdating = gv_RowUpdating onselectedindexchanged = < span class =code-keyword> gv_SelectedIndexChanged >
< ; 列 >
< asp:TemplateField HeaderText = ID >
< itemtemplate >
< asp:标签 ID = lblid runat = server 文字 =' <% #Eval( ID)%> ' >
< / itemtemplate >
< asp:TemplateField HeaderText = 名字 >
< itemtemplate >
< asp:标签 ID = lblfirstname runat = server 正文 =' <% #Eval( firstname)%> ' >
< / itemtemplate >
< edititemtemplate >
< asp:TextBox ID = txtfirstname runat = server 文字 =' <% #Eval( firstname)%> ' >
< / edititemtemplate >
< asp:TemplateField HeaderText = < span class =code-keyword>姓氏 >
< itemtemplate >
< asp:Label ID = lbllastname runat = server 文字 =' <% #Eval( lastname)%> ' >
< / itemtemplate >
< edititemtemplate >
< asp:TextBox ID = txtlastname runat = server 文字 =' <% #Eval( lastname)%> ' > ;
< / edititemtemplate >
< asp :TemplateField >
< itemtemplate >
< asp:LinkButton ID = lbedit runat = server CommandName = 修改 > 编辑
< asp:LinkButton ID = lbdelete runat = 服务器 CommandName = 删除 > 删除
< / itemtemplate >
< edititemtemplate >
< asp:LinkButton ID = lbupdate runat = server CommandName = 更新 > 更新
< asp:LinkButton ID = lbcancel < span class =code-attribute> runat < span class =code-keyword> = server CommandName = 取消 > 取消
< / edititemtemplate >
< /列 >
< < span class =code-leadattribute> / div >
< / form >
< / body >
< / html >
和我的.cs代码是
< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data;
使用 System.Data.Odbc;
public partial class GridView2:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack )
{
Bind();
}
}
public void Bind( )
{
OdbcConnection con = new OdbcConnection( Driver = {Mysql ODBC 5.1 Driver}; server = AFFINEWSDB01; database = training; uid = trainee; password = trainee;);
con.Open();
OdbcCommand cmd = new OdbcCommand( select *来自add2,con);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
con.Close();
}
protected void btnadd_Click( object sender,EventArgs e)
{
Response.Redirect( Add.aspx跨度>);
}
受保护 void gv_RowEditing( object sender,GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
Bind();
}
public void UpdateData( string firstname, string lastname)
{
OdbcConnection con = new OdbcConnection( Driver = {Mysql ODBC 5.1 Driver}; server = AFFINEWSDB01; database = training; uid = trainee;密码=受训者;跨度>);
con.Open();
OdbcCommand cmd = new OdbcCommand( update add2 set Firstname =' + firstname + ',con);
cmd.ExecuteNonQuery();
Bind();
con.Close();
}
受保护 void gv_RowUpdating( object sender,GridViewUpdateEventArgs e)
{
OdbcConnection con = new OdbcConnection( Driver = {Mysql ODBC 5.1 Driver}; server = AFFINEWSDB01; database = training; uid = trainee; password = trainee;) ;
con.Open();
string firstName = gv.DataKeys [e.RowIndex] .Value.ToString();
GridViewRow row = gv.Rows [e.RowIndex];
TextBox lastname =(TextBox)row.FindControl( txtlastname);
UpdateData(firstName,lastname.Text);
con.Close();
}
受保护 void gv_RowCancellingedit( object sender,GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
Bind();
}
public void DeleteDate( int id, string firstname, string lastname)
{
OdbcConnection con = new OdbcConnection( Driver = {Mysql ODBC 5.1 Driver}; server = AFFINEWSDB01; database = training; uid = trainee; password = trainee;);
con.Open();
OdbcCommand cmd = new OdbcCommand( delete来自add2,其中ID =' + id + ',con);
cmd.ExecuteNonQuery();
Bind();
con.Close();
}
protected void gv_SelectedIndexChanged( object sender,EventArgs e)
{
}
protected void gv_RowDeleting( object sender,GridViewDeleteEventArgs e)
{
OdbcConnection con = new OdbcConnection( Driver = {Mysql ODBC 5.1 Driver}; server = AFFINEWSDB01;数据库=培训; UID =受训者;密码=受训者;跨度>);
con.Open();
string firstName = gv.DataKeys [e.AffectedRows] .Value.ToString();
GridViewRow row = gv.Rows [e.AffectedRows];
OdbcCommand cmd = new OdbcCommand( delete来自add2,其中Firstname =' + firstName + ',con);
cmd.ExecuteNonQuery();
Bind();
con.Close();
}
}
我的gridview绑定但编辑和删除操作无效。
请帮帮我。
cmd = new MySqlCommand( 从add2中删除ID = @ id,con );
// 添加删除记录的参数。
cmd.Parameters。 AddWithValue( @ id,GridView1.Rows [e.RowIndex] .Cells [更改ID Colunm的索引] .Text);
嗨...
请在你的删除()中使用像打击一样。
cmd = new MySqlCommand( 从add2中删除ID = @ id,con);
// 添加删除记录的参数。
cmd.Parameters。 AddWithValue( @ id,GridView1.Rows [e.RowIndex] .Cells [ 1 ]。文字);
和编辑()这样的
gv.EditIndex = e.NewEditIndex;
它可能对你有帮助。
谢谢你。
good evening,
i am doing insert,edit delete by using gridview.
my aspx code id
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView2.aspx.cs" Inherits="GridView2" %>
<!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>
<asp:Button ID="btnadd" runat="server" Text="Add" onclick="btnadd_Click" />
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false"
onrowdeleting="gv_RowDeleting" onrowediting="gv_RowEditing"
onrowupdating="gv_RowUpdating" onselectedindexchanged="gv_SelectedIndexChanged">
<columns>
<asp:TemplateField HeaderText="ID">
<itemtemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("ID") %>'>
</itemtemplate>
<asp:TemplateField HeaderText="Firstname">
<itemtemplate>
<asp:Label ID="lblfirstname" runat="server" Text='<%# Eval("firstname") %>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtfirstname" runat="server" Text='<%# Eval("firstname") %>'>
</edititemtemplate>
<asp:TemplateField HeaderText="Lastname">
<itemtemplate>
<asp:Label ID="lbllastname" runat="server" Text='<%# Eval("lastname") %>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="txtlastname" runat="server" Text='<%# Eval("lastname") %>'>
</edititemtemplate>
<asp:TemplateField>
<itemtemplate>
<asp:LinkButton ID="lbedit" runat="server" CommandName="Edit">Edit
<asp:LinkButton ID="lbdelete" runat="server" CommandName="Delete">Delete
</itemtemplate>
<edititemtemplate>
<asp:LinkButton ID="lbupdate" runat="server" CommandName="Update">Update
<asp:LinkButton ID="lbcancel" runat="server" CommandName="Cancel">Cancel
</edititemtemplate>
</columns>
</div>
</form>
</body>
</html>
and my .cs code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;
public partial class GridView2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
con.Open();
OdbcCommand cmd = new OdbcCommand("select * from add2", con);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
con.Close();
}
protected void btnadd_Click(object sender, EventArgs e)
{
Response.Redirect("Add.aspx");
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
Bind();
}
public void UpdateData(string firstname, string lastname)
{
OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
con.Open();
OdbcCommand cmd = new OdbcCommand("update add2 set Firstname='" + firstname + "'", con);
cmd.ExecuteNonQuery();
Bind();
con.Close();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
con.Open();
string firstName = gv.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = gv.Rows[e.RowIndex];
TextBox lastname = (TextBox)row.FindControl("txtlastname");
UpdateData(firstName, lastname.Text);
con.Close();
}
protected void gv_RowCancellingedit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
Bind();
}
public void DeleteDate(int id, string firstname, string lastname)
{
OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
con.Open();
OdbcCommand cmd = new OdbcCommand("delete from add2 where ID='" + id + "'", con);
cmd.ExecuteNonQuery();
Bind();
con.Close();
}
protected void gv_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
OdbcConnection con = new OdbcConnection("Driver={Mysql ODBC 5.1 Driver};server=AFFINEWSDB01;database=training;uid=trainee;password=trainee;");
con.Open();
string firstName = gv.DataKeys[e.AffectedRows].Value.ToString();
GridViewRow row = gv.Rows[e.AffectedRows];
OdbcCommand cmd = new OdbcCommand("delete from add2 where Firstname='" + firstName + "' ", con);
cmd.ExecuteNonQuery();
Bind();
con.Close();
}
}
my gridview is binding but edit and delete operations are not working.
please help me.
cmd = new MySqlCommand("delete from add2 where ID=@id", con); // Adding the parameter for deleting the record. cmd.Parameters.AddWithValue("@id", GridView1.Rows[e.RowIndex].Cells[Change Indexof ID Colunm ].Text);
Hi...
See in your delete() use like blow.
cmd = new MySqlCommand("delete from add2 where ID=@id", con); // Adding the parameter for deleting the record. cmd.Parameters.AddWithValue("@id", GridView1.Rows[e.RowIndex].Cells[1].Text);
and for edit() like this
gv.EditIndex = e.NewEditIndex;
its may helpful for u.
thank u.
这篇关于使用gridview插入编辑删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!