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

查看:61
本文介绍了如何在GridView中编辑,删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,
请给我建议C#代码..用于在gridview中编辑和删除记录
我需要通过单击这些按钮来删除,编辑.
这是我的设计页面

Dear Experts,
Plz can you Suggest me C# code..For Edit and Delete records in gridview
I need to delete ,edit by clicking those buttons.
This is my design page

<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="5" OnRowEditing="gvdetails_Editing" DataKeyNames="username"

        OnRowDeleting="gvdetails_Deleting" AutoGenerateEditButton="true"

            AutoGenerateDeleteButton="true"

            onselectedindexchanged="gvdetails_SelectedIndexChanged">
        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" />
        <Columns>
        <asp:BoundField DataField="UserName" HeaderText="UserName" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Designation" HeaderText="Designation" />

        </Columns>

        </asp:GridView>
        <asp:Label ID="lblresult" runat="server"></asp:Label>

推荐答案



请查看以下链接.这将对您有所帮助.

使用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/ [
Hi,

Please review the below link. It will be helpful to you.

Insert, Update, Delete with Gridview ... Simple Way[^]
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/[^]

Thanks,
Viprat


朋友,

这将有助于U !!!!!!!!!

Friend,

This will help U!!!!!!!!!

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</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="White"

onrowcancelingedit="gvDetails_RowCancelingEdit"

onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"

onrowupdating="gvDetails_RowUpdating"

onrowcommand="gvDetails_RowCommand">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("Username") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("UserName") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrusrname" runat="server"/>
<asp:RequiredFieldValidator ID="rfvusername" runat="server" ControlToValidate="txtftrusrname" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%#Eval("City") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrcity" runat="server"/>
<asp:RequiredFieldValidator ID="rfvcity" runat="server" ControlToValidate="txtftrcity" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<EditItemTemplate>
<asp:TextBox ID="txtDesg" runat="server" Text='<%#Eval("Designation") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDesg" runat="server" Text='<%#Eval("Designation") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrDesignation" runat="server"/>
<asp:RequiredFieldValidator ID="rfvdesignation" runat="server" ControlToValidate="txtftrDesignation" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>





SqlConnection con =新的SqlConnection(数据源= SureshDasari;集成安全性= true;初始目录= MySampleDB");
受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
{
BindEmployeeDetails();
}
}
受保护的void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd =新的SqlCommand(从Employee_Details中选择*",con);
SqlDataAdapter da =新的SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
如果(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 =未找到记录";
}
}
受保护的void gvDetails_RowEditing(对象发送者,GridViewEditEventArgs e)
{
gvDetails.EditIndex = e.NewEditIndex;
BindEmployeeDetails();
}
受保护的void gvDetails_RowUpdating(对象发送者,GridViewUpdateEventArgs e)
{
int用户ID = Convert.ToInt32(gvDetails.DataKeys [e.RowIndex] .Value.ToString());
字符串用户名= gvDetails.DataKeys [e.RowIndex] .Values ["UserName"].ToString();
TextBox txtcity =(TextBox)gvDetails.Rows [e.RowIndex] .FindControl("txtcity");
TextBox txtDesignation =(TextBox)gvDetails.Rows [e.RowIndex] .FindControl("txtDesg");
con.Open();
SqlCommand cmd =新的SqlCommand("update Employee_Details set City =""+ txtcity.Text +"'',Designation =" + txtDesignation.Text +''其中UserId =" + userid,con));
cmd.ExecuteNonQuery();
con.Close();
lblresult.ForeColor =颜色.绿色;
lblresult.Text =用户名+详细信息已成功更新";
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
受保护的void gvDetails_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
{
gvDetails.EditIndex = -1;
BindEmployeeDetails();
}
受保护的void gvDetails_RowDeleting(对象发送者,GridViewDeleteEventArgs e)
{
int userid = Convert.ToInt32(gvDetails.DataKeys [e.RowIndex] .Values ["UserId"].ToString());
字符串用户名= gvDetails.DataKeys [e.RowIndex] .Values ["UserName"].ToString();
con.Open();
SqlCommand cmd =新的SqlCommand(从Employee_Details中删除,其中UserId =" + userid,con);
int结果= cmd.ExecuteNonQuery();
con.Close();
如果(结果== 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Red;
lblresult.Text =用户名+详细信息已成功删除";
}
}
受保护的void gvDetails_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname =(TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
TextBox txtCity =(TextBox)gvDetails.FooterRow.FindControl("txtftrcity");
TextBox txtDesgnation =(TextBox)gvDetails.FooterRow.FindControl("txtftrDesignation");
con.Open();
SqlCommand cmd =
新的SqlCommand(
插入Employee_Details(UserName,City,Designation)values(""+ txtUsrname.Text +"''," +
txtCity.Text +'',""+ txtDesgnation.Text +"''),con);
int result = cmd.ExecuteNonQuery();
con.Close();
if(结果== 1)
{
BindEmployeeDetails();
lblresult.ForeColor =颜色.绿色;
lblresult.Text = txtUsrname.Text +成功插入详细信息";
}
其他
{
lblresult.ForeColor = Color.Red;
lblresult.Text = txtUsrname.Text +未插入详细信息";
}
}
}


问候,
Karthik.J





SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
protected void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Employee_Details", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
else
{
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 = "No Records Found";
}
}
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());
string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
TextBox txtcity = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtcity");
TextBox 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 = Color.Green;
lblresult.Text = username + " Details Updated successfully";
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());
string username = gvDetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from Employee_Details where UserId=" + userid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Red;
lblresult.Text = username + " details deleted successfully";
}
}
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
TextBox txtCity = (TextBox)gvDetails.FooterRow.FindControl("txtftrcity");
TextBox txtDesgnation = (TextBox) gvDetails.FooterRow.FindControl("txtftrDesignation");
con.Open();
SqlCommand cmd =
new SqlCommand(
"insert into Employee_Details(UserName,City,Designation) values(''" + txtUsrname.Text + "'',''" +
txtCity.Text + "'',''" + txtDesgnation.Text + "'')", con);
int result= cmd.ExecuteNonQuery();
con.Close();
if(result==1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Green;
lblresult.Text = txtUsrname.Text + " Details inserted successfully";
}
else
{
lblresult.ForeColor = Color.Red;
lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}


Regards,
Karthik.J


浏览该网址

编辑,删除记录

编辑,删除记录

祝你好运
Go through this Url

Edit,delete records

Edit,delete records

good luck


这篇关于如何在GridView中编辑,删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆