C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Editing, Deleting Gridview in VB.net 2.0


Posted Date: 06 Aug 2008    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: arjun       Member Level: Bronze
Rating:     Points: 10



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




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Editing  .  Deleting Gridview in vb.net 2.0  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Dynamically adding columns to the GridView control
Previous Resource: Exporting DataGrid to Excel in c#
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use