944,131 Members | Top Members by Rank

Ad:
Jun 5th, 2007
0

how to write code for update,delete,cancel buttons in datagrid using vb.net only..plz

Expand Post »
can anybody help m-how to write code for update,delete,cancel buttons on datgrid...using vb.net only plz its urgent help m(only in code-behind file)........plz plzzzzzzzzzz
Reputation Points: 5
Solved Threads: 1
Junior Poster in Training
preetham.saroja is offline Offline
82 posts
since Jun 2007
Sep 27th, 2007
0

Re: how to write code for update,delete,cancel buttons in datagrid using vb.net only..plz

<asp:datagrid id="dg1" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 112px" runat="server"
AutoGenerateColumns="False" dataKeyField="SNo" OnEditCommand="DoItemEdit" OnDeleteCommand="DoItemDelete"
OnUpdateCommand="DoItemUpdate">
<Columns>
<asp:BoundColumn Visible="False" DataField="SNo" HeaderText="SNO"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="name" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Chemical Name">
<ItemTemplate>
<asp:Label id="Label3" Text='<%# Container.DataItem("name") %>' Runat="server">
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtachemical" Runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox id="txtchemical" runat="server" Text='<%# container.dataitem("name") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton id="LinkButton1" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Modify">
<ItemTemplate>
<asp:LinkButton id="Linkbutton2" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button CommandName="Insert" Text="Add" ID="btnAdd" Runat="server" />
</FooterTemplate>
<EditItemTemplate>
<asp:LinkButton id="LinkButton3" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>&nbsp;&nbsp;&nbsp;&nbsp;
<asp:LinkButton id="Linkbutton4" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>




Place the code below in codebehind


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack Then
cn = New SqlConnection("server=sdrnsrv;uid=sdrn;pwd=sdrn;database=test")
cn.Open()
qstring = "select * from test"
cmd = New SqlCommand(qstring, cn)
adp = New SqlDataAdapter
ds = New DataSet
adp.SelectCommand = cmd
adp.Fill(ds)
dg1.DataSource = ds
dg1.DataBind()
cn.Close()
End If
End Sub

Sub DoItemDelete(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
sno = dg1.DataKeys(objArgs.Item.ItemIndex)
cn = New SqlConnection("server=sdrnsrv;uid=sdrn;pwd=sdrn;database=test")
cn.Open()
qstring = "delete from test where sno=" & sno
cmd = New SqlCommand(qstring, cn)
cmd.ExecuteNonQuery()

qstring = "select * from test"
cmd = New SqlCommand(qstring, cn)
adp = New SqlDataAdapter
ds = New DataSet
adp.SelectCommand = cmd
adp.Fill(ds)
dg1.DataSource = ds
dg1.DataBind()
cn.Close()
End Sub
Sub DoItemEdit(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dg1.EditItemIndex = objArgs.Item.ItemIndex
End Sub

Sub DoItemCancel(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
dg1.EditItemIndex = -1
End Sub
Sub DoItemUpdate(ByVal objSource As Object, ByVal objArgs As DataGridCommandEventArgs)
sname = CType(objArgs.Item.FindControl("txtchemical"), TextBox)
sno = dg1.DataKeys(objArgs.Item.ItemIndex)
cn = New SqlConnection("server=sdrnsrv;uid=sdrn;pwd=sdrn;database=test")
cn.Open()

qstring = "update test set name='" & sname.text & "' where sno=" & sno
cmd = New SqlCommand(qstring, cn)
cmd.ExecuteNonQuery()
dg1.EditItemIndex = -1
qstring = "select * from test"
cmd = New SqlCommand(qstring, cn)
adp = New SqlDataAdapter
ds = New DataSet
adp.SelectCommand = cmd
adp.Fill(ds)
dg1.DataSource = ds
dg1.DataBind()
cn.Close()
End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
altafnazir is offline Offline
1 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in IT Professionals' Lounge Forum Timeline: IT or ComSci?? plss,, help
Next Thread in IT Professionals' Lounge Forum Timeline: end of lline control panel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC