Protected ss As String = "SELECT * from T_ORG_EXT"
Dim bEditing As Boolean = False
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
If Not Page.IsPostBack Then
Label1.Text = "ASC"
BindGrid()
End If
End Sub
Sub BindGrid()
'Dim sMapPath As String
'sMapPath = Server.MapPath("db\teste.mdb"
'cs = cs & sMapPath
Dim dc As New OleDb.OleDbConnection(cs)
Dim da As New OleDb.OleDbDataAdapter(ss, dc)
Dim ds As New DataSet()
da.Fill(ds)
da.Fill(ds, "T_ORG_EXT"
Dim dv As DataView = ds.Tables("T_ORG_EXT".DefaultView
If Label2.Text <> "" Then dv.Sort = Label2.Text + " " + Label1.Text
dg.DataSource = dv
dg.DataBind()
End Sub
Sub dg_Page(ByVal Sender As Object, ByVal E As DataGridPageChangedEventArgs)
If Not bEditing Then
dg.CurrentPageIndex = E.NewPageIndex
BindGrid()
End If
End Sub
Sub dg_Sort(ByVal Sender As Object, ByVal E As DataGridSortCommandEventArgs)
If Not bEditing Then
If Label2.Text = E.SortExpression Then
If Label1.Text = "ASC" Then
Label1.Text = "DESC"
Else
Label1.Text = "ASC"
End If
End If
Label2.Text = E.SortExpression
BindGrid()
End If
End Sub
Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
If Not bEditing Then
dg.EditItemIndex = e.Item.ItemIndex
BindGrid()
SingleColumn(False)
End If
End Sub
Sub dg_Cancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
dg.EditItemIndex = -1
BindGrid()
SingleColumn(True)
End Sub
Sub dg_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
dg.EditItemIndex = -1
Dim strcmd As String = "UPDATE T_ORG_EXT SET "
Dim pkvalue As String = dg.DataKeys(CInt(e.Item.ItemIndex))
strcmd = strcmd & " WHERE ROW_ID = " & pkvalue
Dim dc As New OleDb.OleDbConnection(cs)
Dim cmd As New OleDb.OleDbCommand(strcmd, dc)
Try
dc.Open()
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
Label3.Text = ex.ToString()
Finally
dc.Close()
End Try
BindGrid()
SingleColumn(True)
End Sub
Sub dg_Delete(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
If Not bEditing Then
Dim pkvalue As String = dg.DataKeys(CInt(E.Item.ItemIndex))
Dim dc As New OleDb.OleDbConnection(cs)
Dim cmd As New OleDb.OleDbCommand("DELETE from T_ORG_EXT where ROW_ID = " & pkvalue, dc)
dc.Open()
cmd.ExecuteNonQuery()
dc.Close()
dg.CurrentPageIndex = 0
dg.EditItemIndex = -1
BindGrid()
End If
End Sub
Sub dg_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim deletebutton As WebControl = CType(e.Item.Cells(dg.Columns.Count - 1).Controls(0), WebControl)
deletebutton.Attributes.Add("onclick", _
"return confirm('Are you sure you want to delete this record?');"
End If
End Sub
Sub dg_Add(ByVal Sender As Object, ByVal E As EventArgs)
CheckEditing(""
If Not bEditing Then
Dim dc As New OleDb.OleDbConnection(cs)
Dim cmd As New OleDb.OleDbCommand("INSERT into T_ORG_EXT(NAME,AMT) Values ('','')", dc)
dc.Open()
cmd.ExecuteNonQuery()
dc.Close()
dg.CurrentPageIndex = dg.PageCount - 1
BindGrid()
SingleColumn(False)
dg.EditItemIndex = dg.Items.Count - 1
BindGrid()
End If
End Sub
Sub SingleColumn(ByVal b As Boolean)
Dim i As Integer
For i = 2 To dg.Columns.Count - 1
dg.Columns(i).Visible = b
Next
Label3.Text = ""
End Sub
Sub dg_Item(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs)
CheckEditing(E.CommandName)
End Sub
Sub CheckEditing(ByVal commandName As String)
If dg.EditItemIndex <> -1 Then
If commandName <> "Cancel" And commandName <> "Update" Then
Label3.Text = "Please click update to save your changes, or cancel to discard your changes, before selecting another item."
bEditing = True
End If
End If
End Sub
I don't know if it is necessary to create the oledbconnection and the oledbadapter on the design view or if only on the code is enough.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.