Невозможно удалить строку gridview с помощью события команды
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Emp.aspx.cs" Inherits="Test17716.Emp" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td>Name</td> <td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td> </tr> <tr> <td>Address</td> <td> <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox></td> </tr> <tr> <td>Qualification</td> <td> <asp:DropDownList ID="ddlqualification" runat="server"> <%-- <asp:ListItem Text="mca" Value="1"></asp:ListItem> <asp:ListItem Text="mba" Value="2"></asp:ListItem> <asp:ListItem Text="b.tech" Value="3"></asp:ListItem> <asp:ListItem Text="bca" Value="4"></asp:ListItem>--%> </asp:DropDownList></td> </tr> <tr> <td>Gender</td> <td> <asp:RadioButtonList ID="rdbGender" runat="server" RepeatColumns="2"> <%--<asp:ListItem Text="MALE" Value="1"></asp:ListItem>--%> </asp:RadioButtonList></td> </tr> <tr> <td>Hobies</td> <td> <asp:CheckBoxList ID="Chklist" runat="server" RepeatColumns="3"></asp:CheckBoxList></td> </tr> <tr> <td>Files</td> <td> <asp:FileUpload ID="fufile" runat="server"></asp:FileUpload></td> </tr> <tr> <td></td> <td> <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td> </tr> <tr> <td></td> <td> <asp:GridView ID="grd" DataKeyNames="Eid" runat="server" OnRowCommand="grd_RowCommand" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <a href="employeeDtaials.aspx?Eid=<%#Eval("Eid") %>"><%#Eval("Name") %></a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <%#Eval("Address") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Qualification"> <ItemTemplate> <%#Eval("QName") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Gender"> <ItemTemplate> <%#Eval("Gender") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Hobbies"> <ItemTemplate> <%#Eval("Hobbies") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FileName"> <ItemTemplate> <%--<a href="/KB/answers/employeeDtaials.aspx"?Eid=<%#Eval("Eid") %>">--%> <img src="/Files/<%#Eval("FileName") %>" style="width: 50px"> </a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Action"> <ItemTemplate> <asp:LinkButton ID="lnkEdit" Text="View" CommandArgument='<%#Eval("Eid") %>' CommandName="EDT" runat="server"></asp:LinkButton> <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%#Eval("Eid") %>' CommandName="DEL" runat="server"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> </table> </div> </form> </body> </html>
Код CS идет сюда:
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.SqlClient; using System.Configuration; using System.IO; namespace Test17716 { public partial class Emp : System.Web.UI.Page { SqlCommand cmd; SqlDataAdapter da; SqlDataReader dr; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Fill_Qualification(); RdbBind(); Hobies(); fill_Grid(); } } public void Fill_Qualification() { con.Open(); SqlCommand cmd = new SqlCommand("usp_Qualification_Select", con); cmd.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); SqlDataAdapter sdr = new SqlDataAdapter(cmd); sdr.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { ddlqualification.DataValueField = "QID"; ddlqualification.DataTextField = "QName"; ddlqualification.DataSource = ds; ddlqualification.DataBind(); ddlqualification.Items.Insert(0,new ListItem("--Select--","0")); } con.Close(); } public void Hobies() { con.Open(); SqlCommand cmd = new SqlCommand("Myhobies", con); cmd.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { Chklist.DataValueField = "HID"; Chklist.DataTextField = "HobiesNAme"; Chklist.DataSource = ds; Chklist.DataBind(); } con.Close(); } public void RdbBind() { con.Open(); SqlCommand cmd = new SqlCommand("GenderBind", con); cmd.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { rdbGender.DataValueField = "GID"; rdbGender.DataTextField = "GName"; rdbGender.DataSource = ds; rdbGender.DataBind(); } con.Close(); } public void fill_Grid() { con.Open(); SqlCommand cmd = new SqlCommand("usp_Emp_Get", con); cmd.CommandType = CommandType.StoredProcedure; DataSet ds = new DataSet(); SqlDataAdapter sdr = new SqlDataAdapter(cmd); sdr.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { grd.DataSource = ds; grd.DataBind(); } con.Close(); } protected void btnsave_Click(object sender, EventArgs e) { string HOB = ""; for (int i = 0; i < Chklist.Items.Count; i++) { if (Chklist.Items[i].Selected == true) { HOB += Chklist.Items[i].Text + ","; } } HOB = HOB.TrimEnd(','); string FN = Path.GetFileName(fufile.PostedFile.FileName); fufile.SaveAs(Server.MapPath("Files" + "\\" + FN)); con.Open(); SqlCommand cmd = new SqlCommand("usp_Emp_Insert", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", txtname.Text); cmd.Parameters.AddWithValue("@Address", txtaddress.Text); cmd.Parameters.AddWithValue("@Qualification", ddlqualification.SelectedValue); cmd.Parameters.AddWithValue("@GID", rdbGender.SelectedValue); cmd.Parameters.AddWithValue("@Hobbies", HOB); cmd.Parameters.AddWithValue("@FileName", FN); cmd.ExecuteNonQuery(); con.Close(); fill_Grid(); txtaddress.Text = ""; txtname.Text = ""; } protected void grd_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "DEL") { con.Open(); LinkButton lnkbtn = sender as LinkButton; GridViewRow grdRow = lnkbtn.NamingContainer as GridViewRow; string EmpId = grd.DataKeys[grdRow.RowIndex].Value.ToString(); cmd = new SqlCommand("sp_Emp_Delete",con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Id", EmpId); cmd.ExecuteNonQuery(); con.Close(); } else { } } } }
Пожалуйста, помогите мне. Я запутался, как использовать событие команды строки GridView.
Что я уже пробовал:
Я использовал событие команды grid view row, но я не могу удалить строку.
Karthik_Mahalingam
Пожалуйста, не сбрасывайте код.
добавьте только ту часть кода, которая связана с вашей проблемой, чтобы пользователи могли заглянуть в нее.