Hi, I have try to insert edit and delete feature in my gridview app. however, the code is not really work. when I click the edit or delete button it keep adding the blank line. It supposed show textbox with data and Update Cancel link button, when I click the edit button.
here my html code
<form id="form1" runat="server">
<asp:GridView ID="GridView1" DataKeyNames="ModuleID" runat="server" AutoGenerateColumns="False"
ShowFooter="True" OnRowCommand="Add" onrowcancelingedit="GridView1_RowCancelling" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<HeaderStyle BackColor="#9a9a9a" ForeColor="White" Font-Bold="true" Height="30" />
<AlternatingRowStyle BackColor="#f5f5f5" />
<Columns>
<asp:TemplateField HeaderText="Module Name">
<ItemTemplate>
<%# Eval("ModuleName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtModuleName" Text='<%# Eval("ModuleName") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdModuleName"
ControlToValidate="txtModuleName" ValidationGroup="ModuleName" ErrorMessage="*" />
</EditItemTemplate>
<asp:TemplateField HeaderText="Sub Module">
<ItemTemplate>
<%# Eval("SubModule") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSubModule" Text='<%# Eval("SubModule") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdSubModule"
ControlToValidate="txtSubModule" ValidationGroup="SubModule" ErrorMessage="*" />
</EditItemTemplate>
<asp:TemplateField HeaderText="Add">
<ItemTemplate>
<%# Eval("TagAdd") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlTagAdd" Text='<%# Eval("TagAdd") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfdTagAdd"
ControlToValidate="ddlTagAdd" ValidationGroup="TagAdd" ErrorMessage="*" />
</EditItemTemplate>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
and here is code behind
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindData()
End Sub
Protected Sub GridView1_RowCancelling(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
Me.BindData()
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim ModuleID As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("lblModuleID")), Label).Text
Dim ModuleName As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtModuleName")), TextBox).Text
Dim SubModule As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtSubModule")), TextBox).Text
Dim TagAdd As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("ddlTagAdd")), DropDownList).SelectedValue.ToString
Dim TagEdit As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("ddlTagEdit")), DropDownList).SelectedValue.ToString
Dim TagDelete As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("ddlTagDelete")), DropDownList).SelectedValue.ToString
Dim strConnString As String = ConfigurationManager.ConnectionStrings("AMS").ConnectionString
Using con As SqlConnection = New SqlConnection(strConnString)
Using cmd As SqlCommand = New SqlCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "UPDATE ModuleDetail SET ModuleName = @ModuleName, SubModule=@SubModule, TagAdd = @TagAdd, TagEdit = @TagEdit, TagDelete = @TagDelete where ModuleID=@ModuleID SELECT ModuleName, SubModule, TagAdd, TagEdit, TagDelete FROM ModuleDetail"
cmd.Parameters.AddWithValue("@ModuleName", ModuleName)
cmd.Parameters.AddWithValue("@SubModule", SubModule)
cmd.Parameters.AddWithValue("@TagAdd", TagAdd.ToString)
cmd.Parameters.AddWithValue("@TagEdit", TagEdit.ToString)
cmd.Parameters.AddWithValue("@TagDelete", TagDelete.ToString)
GridView1.EditIndex = -1
GridView1.DataSource = cmd
GridView1.DataBind()
End Using
End Using
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As EventArgs)
Dim lnkRemove As LinkButton = DirectCast(sender, LinkButton)
Dim ModuleName As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtModuleName")), TextBox).Text
Dim SubModule As String = DirectCast((GridView1.Rows(GridView1.EditIndex).FindControl("txtSubModule")), TextBox).Text
Dim strConnString As String = ConfigurationManager.ConnectionStrings("AMS").ConnectionString
Using con As SqlConnection = New SqlConnection(strConnString)
Using cmd As SqlCommand = New SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "delete from ModuleDetail where ModuleName=@ModuleName, SubModule=@SubModule SELECT ModuleName, SubModule, TagAdd, TagEdit, TagDelete FROM ModuleDetail"
cmd.Parameters.AddWithValue("@ModuleName", ModuleName)
cmd.Parameters.AddWithValue("@SubModule", SubModule)
GridView1.DataSource = cmd
GridView1.DataBind()
End Using
End Using
End Sub
the issues is it show logic error,which it add the blank line everytime I click edit or delete button.