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
preetham.saroja -5 Junior Poster in Training
altafnazir 0 Newbie Poster
<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
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.