0

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

2
Contributors
1
Reply
3
Views
10 Years
Discussion Span
Last Post by altafnazir
0

<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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.