如何在gridview中编辑、删除记录[英] How to edit ,delete records in gridview

本文是小编为大家收集整理的关于如何在gridview中编辑、删除记录的处理方法,想解了如何在gridview中编辑、删除记录的问题怎么解决?如何在gridview中编辑、删除记录问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

尊敬的专家,
请你能给我推荐 C# 代码..在 gridview 中编辑和删除记录
我需要删除,点击这些按钮进行编辑.
这是我的设计页面

<asp:GridView ID="gvdetails"span> runat="服务器" AutoGenerateColumns="false" AllowPaging="true" PageSize="5" OnRowEditing="gvdetails_Editing" DataKeyNames="用户名"        OnRowDeleting="gvdetails_Deleting" AutoGenerateEditButton="true"            AutoGenerateDeleteButton="true"            onselectedindexchanged="gvdetails_SelectedIndexChanged"><PagerSettings 模式="NumericFirstLast" PageButtonCount="5" FirstPageText="首页" NextPageText="下一页" /><><asp:BoundField DataField="UserName" HeaderText="用户名" /><asp:BoundField DataField="City" HeaderText="城市" /><asp:BoundField DataField="Designation" HeaderText="指定" /></Columns></asp:GridView><asp:Label ID="lblresult" runat="服务器"></asp:标签>
解决方案
你好,

请查看以下链接.对你有帮助.

使用Gridview插入、更新、删除...简单方式[^]
http://weblogs.asp.net/manojkdotnet/archive/2009/10/11/insert-update-and-delete-using-grid-view-and-objectdatasource.aspx[^]
http://howtouseasp.net/how-to-use-gridview-with-insert-edit-update-delete-the-ado-net-way-c/[^]

谢谢,
Viprat

朋友,

这对你有帮助!!!!!!!!!

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>无标题页面</title><style type="text/css">.网格视图{字体系列:Verdana;字体大小:10pt;字体粗细:正常;颜色:黑色;}</style></head><body><form id="form1" runat="server"><div><asp:GridView ID="gvDetails" DataKeyNames="UserId,UserName" runat="server"AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="白色"onrowcancelingedit="gvDetails_RowCancelingEdit"onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"onrowupdating="gvDetails_RowUpdating"onrowcommand="gvDetails_RowCommand"><><asp:TemplateField><EditItemTemplate><asp:ImageButton ID="imgbtnUpdate" CommandName="更新" runat="server" ImageUrl="~/Images/update.jpg" 工具提示="更新" 高度="20px" 宽度="20px" /><asp:ImageButton ID="imgbtnCancel" runat="服务器" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" 工具提示="取消" 高度="20px" 宽度="20px" /></EditItemTemplate><ItemTemplate><asp:ImageButton ID="imgbtnEdit" CommandName="编辑" runat="server" ImageUrl="~/Images/Edit.jpg" 工具提示="编辑" 高度="20px" 宽度="20px" /><asp:ImageButton ID="imgbtnDelete" CommandName="删除" 文本="编辑" runat="server" ImageUrl="~/Images/delete.jpg" 工具提示="删除" 高度="20px" 宽度="20px" /></ItemTemplate><FooterTemplate><asp:ImageButton ID="imgbtnAdd" runat="服务器" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" 宽度="30px" 高度="30px" 工具提示="添加新用户" ValidationGroup="validaiton" /></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="UserName"><EditItemTemplate><asp:Label ID="lbdeditusr" runat="服务器" 文本='<%#Eval("用户名") %>'/></EditItemTemplate><ItemTemplate><asp:Label ID="lblitemUsr" runat="服务器" 文本='<%#Eval("用户名") %>'/></ItemTemplate><FooterTemplate><asp:TextBox ID="txtftrusrname" runat="服务器"/><asp:RequiredFieldValidator ID="rfvusername" runat="服务器" ControlToValidate="txtftrusrname" 文本="*" ValidationGroup="validaiton"/></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="City"><EditItemTemplate><asp:TextBox ID="txtcity" runat="服务器" 文本='<%#Eval("城市") %>'/></EditItemTemplate><ItemTemplate><asp:Label ID="lblcity" runat="服务器" 文本='<%#Eval("城市") %>'/></ItemTemplate><FooterTemplate><asp:TextBox ID="txtftrcity" runat="服务器"/><asp:RequiredFieldValidator ID="rfvcity" runat="服务器" ControlToValidate="txtftrcity" 文本="*" ValidationGroup="validaiton"/></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="Designation"><EditItemTemplate><asp:TextBox ID="txtDesg" runat="服务器" 文本='<%#Eval("Designation") %>'/></EditItemTemplate><ItemTemplate><asp:Label ID="lblDesg" runat="服务器" 文本='<%#Eval("Designation") %>'/></ItemTemplate><FooterTemplate><asp:TextBox ID="txtftrDesignation" runat="服务器"/><asp:RequiredFieldValidator ID="rfvdesignation" runat="服务器" ControlToValidate="txtftrDesignation" 文本="*" ValidationGroup="validaiton"/></FooterTemplate></asp:TemplateField></Columns></asp:GridView></div><div><asp:Label ID="lblresult" runat="服务器"></asp:标签></div></form></body></html>



SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
受保护的 void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Employee_Details", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
数据集 ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
其他
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvDetails.DataSource = ds;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "未找到记录";
}
}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindEmployeeDetails();
}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
字符串用户名 = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
文本框 txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
文本框 txtDesignation = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDesg");
con.Open();
SqlCommand cmd = new SqlCommand("update Employee_Details set City=''" + txtcity.Text + "'',Designation=''" + txtDesignation.Text + "'' where UserId=" + userid, con);
cmd.ExecuteNonQuery();
con.Close();
lblresult.ForeColor = 颜色.绿色;
lblresult.Text = 用户名 + "详情更新成功";
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
字符串用户名 = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("从 Employee_Details 中删除 UserId=" + userid, con);
int 结果 = cmd.ExecuteNonQuery();
con.Close();
如果(结果 == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = 颜色.红色;
lblresult.Text = 用户名 + "详细信息删除成功";
}
}
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("AddNew"))
{
文本框 txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
文本框 txtCity = (TextBox)gvDetails.FooterRow.FindControl("txtftrcity");
文本框 txtDesgation = (TextBox) gvDetails.FooterRow.FindControl("txtftrDesignation");
con.Open();
SqlCommand cmd =
新的 SqlCommand(
"插入Employee_Details(UserName,City,Designation) values(''" + txtUsrname.Text + "'',''" +
txtCity.Text + "'',''" + txtDesgation.Text + "'')", con);
int 结果= cmd.ExecuteNonQuery();
con.Close();
如果(结果==1)
{
BindEmployeeDetails();
lblresult.ForeColor = 颜色.绿色;
lblresult.Text = txtUsrname.Text + "详情插入成功";
}
其他
{
lblresult.ForeColor = 颜色.红色;
lblresult.Text = txtUsrname.Text + "未插入详细信息";
}
}
}


问候,
Karthik.J

浏览这个网址

编辑、删除记录

编辑、删除记录

祝你好运

本文地址:https://www.itbaoku.cn/post/1355759.html