VB Net untuk Net Programer (Data View) - Printable Version +- Come And Feel the Difference (https://www.gookil.com/x2) +-- Forum: Hobbies and Talents (https://www.gookil.com/x2/forum-52.html) +--- Forum: Computer & Technology (https://www.gookil.com/x2/forum-27.html) +---- Forum: PC Discussions (https://www.gookil.com/x2/forum-34.html) +----- Forum: Windows (https://www.gookil.com/x2/forum-37.html) +----- Thread: VB Net untuk Net Programer (Data View) (/thread-216.html) |
VB Net untuk Net Programer (Data View) - vertigo - 10-29-2010 mengurangi aktifitas/kesibukan pada SQL server dengan mengambil suatu porsi data yang cukup besar dan menyimpannya pada objek DataSet. Selanjutnya proses pencarian dilakukan pada objek DataSet, jadi tidak melibatkan SQL Server lagi. Bila tidak ada kebutuhan untuk mengurangi kesibukan pada SQL Server, Anda tidak harus menggunakan objek DataView. Lakukan saja proses sebagaimana biasanya, yakni setiap proses pencarian langsung dilakukan pada database/sumber data. Oleh karena inti pemanfaatan objek DataView adalah untuk mengurangi aktifitas pada SQL server dalam bentuk pencarian record, pemanfaaatan baris data melalui objek DataView bersifat read only. Penulis tidak menganjurkan Anda untuk melibatkan penggunaan objek DataView pada proses penambahan record maupun penghapusan record bila mungkin, karena akan menyebabkan data yang ada pada objek DataView berubah dan hal ini cukup memusingkan saat penulisan program. Tidak ada hal yang istimewa pada objek ini. Pernyataan baris perintahnya cukup jelas, logis, dan hasil yang ditampilkan cukup mudah dipahami. Berikut ini adalah contoh penggunaan objek DataView program ini,Anda dapat melihat penggunaan filter, sorting, maupun mencari record. Sumber data objek DataView diperoleh dari objek Tabel Data, kemudian DataGrid digunakan untuk menampung serta menampilkan baris data yang telah dipoles untuk ditampilkan pada layar monitor. Baris record yang akan dicari (kolom record) harus menjadi kolom kunci pengurutan (penyortiran). Anda dapat menyortir pada lebih dari satu kolom dengan menambahkan tanda koma di antara kedua kolom yang akan digunakan. Bila proses pencarian berhasil maka akan dikembalikan nilai posisi baris record yang dicari, dimulai dari angka 0. Bila pencarian gagal maka akan dikembalikan nilai –1. Secara default, aktifitas yang dilakukan pada objek DataSet bersifat tidak case sensitive, jadi berhati-hatilah dengan sifat ini. Berikut ini adalah contoh program yang dimaksud. Public Class objDataView Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the ‘InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows ‘Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TextBox3 As System.Windows.Forms.TextBox Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents TextBox6 As System.Windows.Forms.TextBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents TextBox5 As System.Windows.Forms.TextBox Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents TextBox4 As System.Windows.Forms.TextBox Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents DataGrid2 As System.Windows.Forms.DataGrid Friend WithEvents Label9 As System.Windows.Forms.Label Friend WithEvents TextBox8 As System.Windows.Forms.TextBox Friend WithEvents Label8 As System.Windows.Forms.Label Friend WithEvents TextBox7 As System.Windows.Forms.TextBox Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents DataGrid3 As System.Windows.Forms.DataGrid Friend WithEvents TextBox9 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.DataGrid1 = New System.Windows.Forms.DataGrid Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.Label2 = New System.Windows.Forms.Label Me.TextBox3 = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.TextBox6 = New System.Windows.Forms.TextBox Me.Label6 = New System.Windows.Forms.Label Me.TextBox5 = New System.Windows.Forms.TextBox Me.Label5 = New System.Windows.Forms.Label Me.TextBox4 = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.DataGrid2 = New System.Windows.Forms.DataGrid Me.TextBox9 = New System.Windows.Forms.TextBox Me.Label9 = New System.Windows.Forms.Label Me.TextBox8 = New System.Windows.Forms.TextBox Me.Label8 = New System.Windows.Forms.Label Me.TextBox7 = New System.Windows.Forms.TextBox Me.Label7 = New System.Windows.Forms.Label Me.DataGrid3 = New System.Windows.Forms.DataGrid CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.DataGrid3, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'DataGrid1 ' Me.DataGrid1.DataMember = "" Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(0, 0) Me.DataGrid1.Name = "DataGrid1" Me.DataGrid1.Size = New System.Drawing.Size(240, 128) Me.DataGrid1.TabIndex = 0 ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(248, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(144, 16) Me.Label1.TabIndex = 1 ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point (400, 8) Me.TextBox1.Name = "TextBox1" Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" ' 'TextBox2 ' Me.TextBox2.Location = New System.Drawing.Point (400, 40) Me.TextBox2.Name = "TextBox2" Me.TextBox2.TabIndex = 4 Me.TextBox2.Text = "" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(248, 48) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(144, 16) Me.Label2.TabIndex = 3 ' 'TextBox3 ' Me.TextBox3.Location = New System.Drawing.Point (400, 72) Me.TextBox3.Name = "TextBox3" Me.TextBox3.TabIndex = 6 Me.TextBox3.Text = "" ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(248, 80) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(144, 16) Me.Label3.TabIndex = 5 ' 'TextBox6 ' Me.TextBox6.Location = New System.Drawing.Point (400, 224) Me.TextBox6.Name = "TextBox6" Me.TextBox6.TabIndex = 13 Me.TextBox6.Text = "" ' 'Label6 ' Me.Label6.Location = New System.Drawing.Point (248, 232) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(144, 16) Me.Label6.TabIndex = 12 ' 'TextBox5 ' Me.TextBox5.Location = New System.Drawing.Point (400, 192) Me.TextBox5.Name = "TextBox5" Me.TextBox5.TabIndex = 11 Me.TextBox5.Text = "" ' 'Label5 ' Me.Label5.Location = New System.Drawing.Point (248, 200) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(144, 16) Me.Label5.TabIndex = 10 ' 'TextBox4 ' Me.TextBox4.Location = New System.Drawing.Point (400, 160) Me.TextBox4.Name = "TextBox4" Me.TextBox4.TabIndex = 9 Me.TextBox4.Text = "" ' 'Label4 ' Me.Label4.Location = New System.Drawing.Point (248, 168) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(144, 16) Me.Label4.TabIndex = 8 ' 'DataGrid2 ' Me.DataGrid2.DataMember = "" Me.DataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid2.Location = New System.Drawing.Point (0, 152) Me.DataGrid2.Name = "DataGrid2" Me.DataGrid2.Size = New System.Drawing.Size(240, 128) Me.DataGrid2.TabIndex = 7 ' 'TextBox9 ' Me.TextBox9.Location = New System.Drawing.Point (400, 376) Me.TextBox9.Name = "TextBox9" Me.TextBox9.TabIndex = 20 Me.TextBox9.Text = "" ' 'Label9 ' Me.Label9.Location = New System.Drawing.Point (248, 384) Me.Label9.Name = "Label9" Me.Label9.Size = New System.Drawing.Size(144, 16) Me.Label9.TabIndex = 19 ' 'TextBox8 ' Me.TextBox8.Location = New System.Drawing.Point (400, 344) Me.TextBox8.Name = "TextBox8" Me.TextBox8.TabIndex = 18 Me.TextBox8.Text = "" ' 'Label8 ' Me.Label8.Location = New System.Drawing.Point (248, 352) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(144, 16) Me.Label8.TabIndex = 17 'TextBox7 ' Me.TextBox7.Location = New System.Drawing.Point (400, 312) Me.TextBox7.Name = "TextBox7" Me.TextBox7.TabIndex = 16 Me.TextBox7.Text = "" ' 'Label7 ' Me.Label7.Location = New System.Drawing.Point (248, 320) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(144, 16) Me.Label7.TabIndex = 15 ' 'DataGrid3 ' Me.DataGrid3.DataMember = "" Me.DataGrid3.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid3.Location = New System.Drawing.Point (0, 304) Me.DataGrid3.Name = "DataGrid3" Me.DataGrid3.Size = New System.Drawing.Size(240, 128) Me.DataGrid3.TabIndex = 14 ' 'objDataView ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(512, 429) Me.Controls.Add(Me.TextBox9) Me.Controls.Add(Me.Label9) Me.Controls.Add(Me.TextBox8) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.TextBox7) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.DataGrid3) Me.Controls.Add(Me.TextBox6) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.TextBox5) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.TextBox4) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.DataGrid2) Me.Controls.Add(Me.TextBox3) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.DataGrid1) Me.Name = "objDataView" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "objDataView" CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.DataGrid2, System.ComponentModel.ISupportInitialize).EndInit() CType(Me.DataGrid3, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private Sub objDataView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim objConnection As SqlConnection Dim objCommand As SqlCommand Dim objDataAdapter As SqlDataAdapter Dim objDataSet As New DataSet Dim objDataView As New DataView Dim strConn, strSQL As String Dim form_MenuInduk As New MenuInduk strConn = form_MenuInduk.mRoot_StrConn strSQL = "select album_id, album_singer, album_title from AlbumList order by album_id" objConnection = New SqlConnection(strConn) objCommand = New SqlCommand(strSQL, objConnection) objConnection.Open() objDataAdapter = New SqlDataAdapter(objCommand) objDataAdapter.Fill(objDataSet, "mDT_albumlist") ' Set objDataView, data berasal dari objek Tabel objDataView = New DataView(objDataSet.Tables("mDT_AlbumList")) DataGrid1.DataSource = objDataView DataGrid1.CaptionText = "Data tanpa filter" 'album_id harus di sort agar bisa di cari/find objDataView.Sort() = "album_id" Label1.Text = "cari posisi AL0004 :" Dim mPos As Integer = objDataView.Find("AL0004") TextBox1.Text = mPos Label2.Text = "cari posisi AL0001 :" TextBox2.Text = objDataView.Find("AL0001") Label3.Text = "cari posisi ALZZZZ :" TextBox3.Text = objDataView.Find("ALZZZZ") objDataView = New DataView(objDataSet.Tables("mDT_AlbumList")) DataGrid2.DataSource = objDataView DataGrid2.CaptionText = "Filter Singer - Andien" ' bila CaseSensitive() di set True, album_singer 'andien' ' disertakan karena 'andien' <> 'aNdIeN' objDataSet.CaseSensitive() = False objDataView.RowFilter() = "album_singer <> 'aNdIeN' " objDataView.Sort() = "album_id" Label4.Text = "cari posisi AL0004 :" Dim mPos2 As Integer = objDataView.Find("AL0004") TextBox4.Text = mPos2 Label5.Text = "cari posisi AL0001 :" TextBox5.Text = objDataView.Find("AL0001") Label6.Text = "cari posisi ALZZZZ :" TextBox6.Text = objDataView.Find("ALZZZZ") objDataView = New DataView(objDataSet.Tables("mDT_AlbumList")) DataGrid3.DataSource = objDataView DataGrid3.CaptionText = "Data Tanpa Filter, Sort Descending" objDataView.Sort() = "album_id desc" Label7.Text = "cari posisi AL0004 :" Dim mPos3 As Integer = objDataView.Find("AL0004") TextBox7.Text = mPos3 Label8.Text = "cari posisi AL0001 :" TextBox8.Text = objDataView.Find("AL0001") Label9.Text = "cari posisi ALZZZZ :" TextBox9.Text = objDataView.Find("ALZZZZ") objConnection.Close() End Sub End Class |