Hi Friends, Before writing this article, i had practise a lot to find edit and deleting records in gridview. When i searched for this i get information using C# not in VB.net. So I post this after completion in vb.net 2.0
First I have taken a gridview lik this:
<asp:GridView ID="GridView1" runat="server" ShowFooter="True" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="sid" AutoGenerateColumns="False" > <EditRowStyle BackColor="Beige" /> <Columns> <asp:BoundField HeaderText="SID" DataField="sid" /> <asp:TemplateField> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, "sname")%> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat="server" ID="txtSname" Text='<%# DataBinder.Eval(Container.DataItem,"Sname") %>' /> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
In vb.net page on following event I write this code:
On Page load'''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Call fillGrid() End If
End Sub
on fillgrid function'''''''''''''''''''''''''''''''''''''''''''''''' Sub fillGrid() Call DisplayRecord("select * from example") If Datadr.HasRows Then GridView1.DataSource = Datadr GridView1.DataBind() End If Datadr.Close() Datadr = Nothing End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 Call fillGrid() End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim idd As Integer idd = GridView1.DataKeys(e.RowIndex).Value
Dim delCommand As New SqlCommand delCommand = Conn.CreateCommand delCommand.CommandType = CommandType.Text delCommand.CommandText = "delete from example where sid=" & Trim(idd) & " " If Conn.State = ConnectionState.Closed Then Conn.Open() delCommand.ExecuteNonQuery() Conn.Close()
GridView1.EditIndex = -1 Call fillGrid() End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing GridView1.EditIndex = e.NewEditIndex Call fillGrid() End Sub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim idd As Integer idd = GridView1.DataKeys(e.RowIndex).Value
Dim txtsname As TextBox Dim a As String txtsname = GridView1.Rows(e.RowIndex).FindControl("txtSname") a = Trim(txtsname.Text.ToString)
Dim upcommand As New SqlCommand upcommand = Conn.CreateCommand upcommand.CommandType = CommandType.Text upcommand.CommandText = "update example set sname='" & a & "' where sid=" & Trim(idd) & " " If Conn.State = ConnectionState.Closed Then Conn.Open() upcommand.ExecuteNonQuery() Conn.Close()
GridView1.EditIndex = -1 Call fillGrid() End Sub
For more details, visit http://Editing in VB, Grdiding in VB, VB.net snippets
|
No responses found. Be the first to respond and make money from revenue sharing program.
|