TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Riddhi Valecha
444
3.3k
412.6k
Delete A Record From Datatable and Bind DataGrid
Sep 10 2019 7:29 AM
Dear Team,
I need help.
My Requirement is -
1. On Page Load, Bind the DataGrid - Done.
2. There are 2 buttons -"Add New" and "Edit".
"Add New" -> Add a new row in datagrid - Done
"Edit" -> Edit values in Datagrid - Done
3. There is a "Delete" button in Datagrid -> Done
4. On clicking "Delete" button, that particular row must be removed. Logic - If that row is already there in the database, remove from the database and bind the grid. - Done.
If that record is newly add by clicking on "Add New" button and not in Database, remove that record from datatable and bind the Datagrid - NEED HELP.
------------
My Code is as below-
--------
ASPX Page -
-----------------------
<asp:DataGrid ID="dgUltimateHoldingCompany" runat="server" Width="100%" AutoGenerateColumns="False"
PageSize="20" >
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="srnoUltimateHoldingCompany_" ReadOnly="True" HeaderText="srno_"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="rowidUltimateHoldingCompany" ReadOnly="True" HeaderText="ROW_ID"></asp:BoundColumn>
<asp:TemplateColumn Visible="False" HeaderText="rowid">
<ItemTemplate>
<asp:TextBox ID="txtdrowidUltimateHoldingCompany" Visible="False" BorderStyle="Groove" ReadOnly="True"
runat="server" Style="width: 100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Ultimate Holding Entities">
<HeaderStyle Font-Bold="True"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList ID="ddlEntitiesUltimateHoldingCompany" Width="400px" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Delete Record">
<HeaderStyle Font-Bold="true" />
<ItemTemplate>
<asp:Button ID="btndeleteUltimateHoldingCompany" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<br />
<asp:Button ID="btnaddUltimateHoldingCompany" BorderStyle="Groove" Font-Bold="True" runat="server" Text="Add New"
CausesValidation="False"></asp:Button>
<asp:Button ID="btneditUltimateHoldingCompany" BorderStyle="Groove" Width="72px" Font-Bold="True" runat="server"
Text="Edit" CausesValidation="False"></asp:Button>
------------------------------
aspx.vb Page
------------------------
''' <summary>
''' Delete - Ultimate Holding Company
''' </summary>
''' <remarks></remarks>
Private Sub dgUltimateHoldingCompany_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles dgUltimateHoldingCompany.ItemCommand
Try
fn_db()
Dim srno As Integer
srno = e.Item.Cells(0).Text
Dim rowid As String
rowid = e.Item.Cells(1).Text
Dim sql As String
Dim res As String = String.Empty
If rowid <> " " Then
sql = "update ril_mis_ultimateholding set delete_flag = 1 where row_id =" + rowid
res = lobjDatabase.BySingleValue(sql)
GetDataUltimateHoldingCompany()
ElseIf rowid = " " Then ''Delete Record From Datatable.
Dim dtUltimateHoldingCompanyDel As DataTable = CType(Me.ViewState("_dataUltimateHoldingCompany"), DataTable)
Dim drow As DataRow()
drow = dtUltimateHoldingCompanyDel.Select("srnoUltimateHoldingCompany_=" + srno.ToString())
For i As Integer = 0 To drow.Length - 1
dtUltimateHoldingCompanyDel.Rows.Remove(drow(i))
Next
Me.ViewState.Add("_dataUltimateHoldingCompany", dtUltimateHoldingCompanyDel)
dgUltimateHoldingCompany.DataSource = dtUltimateHoldingCompanyDel
dgUltimateHoldingCompany.DataBind()
End If
Catch ex As Exception
End Try
End Sub
------------------
Private Sub dgUltimateHoldingCompany_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgUltimateHoldingCompany.ItemDataBound
fn_db()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim ddlEntitiesUltimateHoldingCompany As DropDownList
Dim txtdrowidUltimateHoldingCompany As TextBox
ddlEntitiesUltimateHoldingCompany = CType(e.Item.FindControl("ddlEntitiesUltimateHoldingCompany"), DropDownList)
' txtdPercentageUltimateHoldingCompany = CType(e.Item.FindControl("txtdPercentageUltimateHoldingCompany"), TextBox)
txtdrowidUltimateHoldingCompany = CType(e.Item.FindControl("txtdrowidUltimateHoldingCompany"), TextBox)
'txtHoldingcompanyAdddate = CType(e.Item.FindControl("txtHoldingcompanyAdddate"), TextBox)
'' Fill Entities List
ddlEntitiesUltimateHoldingCompany.DataSource = Session("dsUltimateHoldingCompany")
ddlEntitiesUltimateHoldingCompany.DataValueField = "e_code"
ddlEntitiesUltimateHoldingCompany.DataTextField = "e_name"
ddlEntitiesUltimateHoldingCompany.DataBind()
ddlEntitiesUltimateHoldingCompany.Items.Insert(0, Selectstring)
If Not (DataBinder.Eval(e.Item.DataItem, "ultimatecode") Is DBNull.Value) Then
Dim i1 As Integer = ddlEntitiesUltimateHoldingCompany.Items.IndexOf(ddlEntitiesUltimateHoldingCompany.Items.FindByValue(e.Item.DataItem("ultimatecode")))
ddlEntitiesUltimateHoldingCompany.SelectedIndex = i1
ddlEntitiesUltimateHoldingCompany.Enabled = False
Else
ddlEntitiesUltimateHoldingCompany.SelectedIndex = 0
End If
If Not (DataBinder.Eval(e.Item.DataItem, "rowidUltimateHoldingCompany") Is DBNull.Value) Then
txtdrowidUltimateHoldingCompany.Text = (e.Item.DataItem("rowidUltimateHoldingCompany").ToString())
txtdrowidUltimateHoldingCompany.Enabled = False
End If
End If
End Sub
-----------------------
Private Sub btnaddUltimateHoldingCompany_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddUltimateHoldingCompany.Click
SaveScreenDataUltimateHoldingCompany()
'Dim i As Integer
Dim dtUltimateHoldingCompany As DataTable = CType(Me.ViewState("_dataUltimateHoldingCompany"), DataTable)
Dim rUltimateHoldingCompany As DataRow = dtUltimateHoldingCompany.NewRow()
dtUltimateHoldingCompany.Rows.Add(rUltimateHoldingCompany)
dgUltimateHoldingCompany.DataSource = dtUltimateHoldingCompany
dgUltimateHoldingCompany.DataBind()
Me.ViewState.Add("_dataUltimateHoldingCompany", dtUltimateHoldingCompany)
End Sub
-----------------
Private Sub SaveScreenDataUltimateHoldingCompany()
Dim dtUltimateHoldingCompany As DataTable = CType(Me.ViewState("_dataUltimateHoldingCompany"), DataTable)
Dim dvUltimateHoldingCompany As DataView = New DataView(dtUltimateHoldingCompany)
Dim txtdrowidUltimateHoldingCompany As TextBox, ddlEntitiesUltimateHoldingCompany As DropDownList
Dim e As DataGridItem
For Each e In dgUltimateHoldingCompany.Items
dvUltimateHoldingCompany.RowFilter = "srnoUltimateHoldingCompany_ = " + e.Cells(0).Text
If dvUltimateHoldingCompany.Count > 0 Then
ddlEntitiesUltimateHoldingCompany = CType(e.FindControl("ddlEntitiesUltimateHoldingCompany"), DropDownList)
'txtdPercentageUltimateHoldingCompany = CType(e.FindControl("txtdPercentageUltimateHoldingCompany"), TextBox)
txtdrowidUltimateHoldingCompany = CType(e.FindControl("txtdrowidUltimateHoldingCompany"), TextBox)
' txtHoldingcompanyAdddate = CType(e.FindControl("txtHoldingcompanyAdddate"), TextBox)
Dim rUltimateHoldingCompany As DataRow = dvUltimateHoldingCompany(0).Row
'==================
If Not (txtdrowidUltimateHoldingCompany.Text.Length <= 0) Then
'If txtdrowid.Text.Length <= 0 Then
rUltimateHoldingCompany("rowidUltimateHoldingCompany") = txtdrowidUltimateHoldingCompany.Text.Trim()
Else
rUltimateHoldingCompany("rowidUltimateHoldingCompany") = DBNull.Value
End If
'===========================================================================
If Not (ddlEntitiesUltimateHoldingCompany.SelectedValue.Trim()) Is DBNull.Value Then
rUltimateHoldingCompany("ultimatecode") = ddlEntitiesUltimateHoldingCompany.SelectedValue.Trim
ddlEntitiesUltimateHoldingCompany.Enabled = False
Else
rUltimateHoldingCompany("ultimatecode") = DBNull.Value
End If
'================================================================
End If
Next
Me.ViewState.Add("_dataUltimateHoldingCompany", dtUltimateHoldingCompany)
End Sub
-----------------
Private Sub btneditUltimateHoldingCompany_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneditUltimateHoldingCompany.Click
Dim dgitemUltimateHoldingCompany As DataGridItem
Dim txtdrowidUltimateHoldingCompany As TextBox
Dim ddlEntitiesUltimateHoldingCompany As DropDownList
For Each dgitemUltimateHoldingCompany In dgUltimateHoldingCompany.Items
txtdrowidUltimateHoldingCompany = CType(dgitemUltimateHoldingCompany.FindControl("txtdrowidUltimateHoldingCompany"), TextBox)
ddlEntitiesUltimateHoldingCompany = CType(dgitemUltimateHoldingCompany.FindControl("ddlEntitiesUltimateHoldingCompany"), DropDownList)
ddlEntitiesUltimateHoldingCompany.Enabled = True
Next
End Sub
------------------
''' <summary>
''' Ultimate Holding Company
''' </summary>
''' <remarks></remarks>
Private Sub GetDataUltimateHoldingCompany()
'Dim sSql As String
Dim dsUltimateHoldingCompanyD As DataSet
Dim sql As String
sql = "select trim(t.row_id) rowidUltimateHoldingCompany , trim(t.selectedentitycode) selectedentitycode , trim(t.selectedentityname) selectedentityname , trim(t.ultimateecode) ultimatecode , trim(t.ultimateename) ultimatename , trim(t.add_by) addbyultimate from ril_mis_ultimateholding t where trim(t.selectedentitycode) = trim('" & txtEntityCode.Text & "') and trim(lower(t.add_by)) = trim(lower('" & Session("UName").ToString() & "')) and trim(t.DELETE_FLAG) = 0 order by t.ultimateename"
fn_db()
dsUltimateHoldingCompanyD = lobjDatabase.ByText(sql)
Session("dsUltimateHoldingCompanyD") = dsUltimateHoldingCompanyD
Dim dcUltimateHoldingCompany As DataColumn
Dim dtUltimateHoldingCompany As DataTable = dsUltimateHoldingCompanyD.Tables(0)
dcUltimateHoldingCompany = New DataColumn("srnoUltimateHoldingCompany_", GetType(Integer))
dtUltimateHoldingCompany.Columns.Add(dcUltimateHoldingCompany)
Dim i As Integer
For i = 0 To dtUltimateHoldingCompany.Rows.Count - 1 Step i + 1
dtUltimateHoldingCompany.Rows(i)("srnoUltimateHoldingCompany_") = i
Next
dcUltimateHoldingCompany.AutoIncrementSeed = i
dcUltimateHoldingCompany.AutoIncrement = True
dtUltimateHoldingCompany.AcceptChanges()
Me.ViewState.Add("_dataUltimateHoldingCompany", dtUltimateHoldingCompany)
dgUltimateHoldingCompany.DataSource = dtUltimateHoldingCompany
dgUltimateHoldingCompany.DataBind()
dsUltimateHoldingCompanyD.Dispose()
End Sub
Reply
Answers (
6
)
ASP.NET Need to write a file on another server
How to get Values