| | |
how to write code for update,delete,cancel buttons in datagrid using vb.net only..plz
![]() |
how to write code for update,delete,cancel buttons in datagrid using vb.net only..plz
0
#1 Jun 5th, 2007
•
•
Join Date: Sep 2007
Posts: 1
Reputation:
Solved Threads: 0
Re: how to write code for update,delete,cancel buttons in datagrid using vb.net only..plz
0
#2 Sep 27th, 2007
<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>
<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
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>
<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
![]() |
Similar Threads
- update,delete,cancel buttons for datagrid (VB.NET)
- How to delete/cancel print job already in queue (Visual Basic 4 / 5 / 6)
- Can we Programaticall Write code in VB6.0, forms/standard module/class code window (Visual Basic 4 / 5 / 6)
- I need to write a C++ Code from the following info. HOW DO I... (C++)
- How do you write a code which compiles in c but not in c++? (C++)
Other Threads in the IT Professionals' Lounge Forum
- Previous Thread: IT or ComSci?? plss,, help
- Next Thread: end of lline control panel
| Thread Tools | Search this Thread |
1gbit advertising advice amazon answers archive british broadband business businessprocesses career carrier censorship cern china cio collectiveintelligence connectivity consumer consumers corporateearnings datatransfer debtcollectors dictionary digg digital ebay ecommerce email employment environment facebook food government grid high-definition hottub infodelivery infotech intel internet interview ipod isp japan kindle lhc library malware marketing mit moonfruit news onlineshopping piracy piratebay pope porn program questions r&d religion remoteworking research retail security sex shopping simple skype smallbusiness smb sms socialmedia socialnetworking software softwareengineer spam speed spending startrek statistics stocks study stumbleupon survey tabletpc technology touch-screen touchscreen twitter uk videoinprint voips web webdeveloper windows words





