ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3958|ตอบกลับ: 1

[VB.NET] การทำคำสั่งทางคณิตศาสตร์กับฟิลด์ข้อมูล (DataColumn Expression)

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6072




DataColumn Expression เป็นการทำคำสั่งทางคณิตศาสตร์กับฟิลด์ข้อมูล (หรือหลัก) เราสามารถกำหนดนิพจน์สำหรับคอลัมน์ใดๆ หรือทำให้มีการคำนวณจากค่าคอลัมน์อื่นๆในแถวเดียวกัน หรือจากค่าคอลัมน์ของหลายๆแถวในตารางก็ได้ โดยตัวอย่างนี้แอดมินจะแสดงให้เห็นการคำนวณแบบง่ายๆ ด้วยการคูณราคาต่อหน่วย และรวมจำนวนระหว่างฟิล์ดข้อมูลเข้าด้วยกัน หรือการบวกจำนวนที่มีอยู่ในสต็อกกับจำนวนที่สั่งซื้อนั่นเอง ...


มาดูโค้ดกันเถอะ ...
  1. #Region "ABOUT"
  2. ' / --------------------------------------------------------------------------
  3. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  4. ' / eMail : thongkorn@hotmail.com
  5. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  6. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  7. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  8. ' / MORE: http://www.g2gnet.com/webboard
  9. ' /
  10. ' / Purpose: Data Column Expression with VB.NET (2010).
  11. ' / Microsoft Visual Basic .NET (2010) + MS Access 2003+
  12. ' /
  13. ' / This is open source code under @CopyLeft by Thongkorn/Common Tubtimkrob.
  14. ' / You can modify and/or distribute without to inform the developer.
  15. ' / --------------------------------------------------------------------------
  16. #End Region

  17. Imports System.Data.OleDb

  18. Public Class frmDataColumn

  19.     ' / --------------------------------------------------------------------------
  20.     Private Sub frmDataColumn_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  21.         '// https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=netframework-4.7.2
  22.         '// Gets or sets the expression used to filter rows, calculate the values in a column, or create an aggregate column.
  23.         Call DataColumnExpressions()
  24.     End Sub

  25.     ' / --------------------------------------------------------------------------
  26.     Sub DataColumnExpressions()
  27.         Dim Conn As New System.Data.OleDb.OleDbConnection
  28.         Dim DA As New System.Data.OleDb.OleDbDataAdapter
  29.         Dim Cmd As New System.Data.OleDb.OleDbCommand
  30.         Dim DC As DataColumn
  31.         Dim DS As New DataSet
  32.         '// Connect to the database
  33.         Conn.ConnectionString = _
  34.             " Provider=Microsoft.ACE.OLEDB.12.0; " & _
  35.             " Data Source=" & _
  36.             MyPath(Application.StartupPath) & "SampleDB.accdb;"
  37.         Cmd.Connection = Conn
  38.         DA.SelectCommand = Cmd

  39.         '// Fill the DataTable
  40.         Cmd.CommandText = "SELECT * FROM Products"
  41.         DA.Fill(DS, "Products")

  42.         '// Add on a few simple expression columns
  43.         DC = New DataColumn("UnitPriceX2")
  44.         DC.DataType = GetType(Double)
  45.         DC.Expression = "UnitPrice * 2" '/ Column and numeric constant.
  46.         DS.Tables("Products").Columns.Add(DC)

  47.         DC = New DataColumn("UnitsInStockUpdate")
  48.         DC.DataType = GetType(Integer)
  49.         DC.Expression = "UnitsInStock" '/ Column UnitsInStock.
  50.         DS.Tables("Products").Columns.Add(DC)

  51.         DC = New DataColumn("UnitsOnOrderUpdate")
  52.         DC.DataType = GetType(Integer)
  53.         DC.Expression = "UnitsOnOrder" '/ Column UnitsOnOrder.
  54.         DS.Tables("Products").Columns.Add(DC)

  55.         '// Summary expression UnitsInStock + UnitsOnOrder.
  56.         DC = New DataColumn("TotalStock")
  57.         DC.DataType = GetType(Integer)
  58.         DC.Expression = "UnitsInStock + UnitsOnOrder" '/ Add two columns.
  59.         DS.Tables("Products").Columns.Add(DC)

  60.         '// Text or String.
  61.         'DC = New DataColumn("New Header")
  62.         'DC.DataType = GetType(String)
  63.         'DC.Expression = "ProductName + ' New Header'" '/ Column and string constant
  64.         'DS.Tables("Products").Columns.Add(DC)
  65.         '//
  66.         dgvData.DataSource = DS.Tables("Products").DefaultView
  67.         '// Organized DataGridView.
  68.         Call SetupDataGridView(dgvData)
  69.         '//
  70.         DS.Dispose()
  71.         DC.Dispose()
  72.         DA.Dispose()
  73.         Cmd.Dispose()
  74.         Conn.Close()
  75.     End Sub

  76.     ' / --------------------------------------------------------------------------
  77.     ' / Organized DataGridView.
  78.     Private Sub SetupDataGridView(ByRef DGV As DataGridView)
  79.         With DGV
  80.             .Columns.Clear()
  81.             .RowTemplate.Height = 26
  82.             .AllowUserToOrderColumns = True
  83.             .AllowUserToDeleteRows = False
  84.             .AllowUserToAddRows = False
  85.             .ReadOnly = True
  86.             .MultiSelect = False
  87.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  88.             .Font = New Font("Tahoma", 8)
  89.             .AlternatingRowsDefaultCellStyle.BackColor = Color.OldLace
  90.             .DefaultCellStyle.SelectionBackColor = Color.SeaGreen
  91.             '.AlternatingRowsDefaultCellStyle.BackColor = Color.LightYellow
  92.             '.DefaultCellStyle.SelectionBackColor = Color.LightBlue
  93.             '/ Auto size column width of each main by sorting the field.
  94.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  95.             .AutoResizeColumns()

  96.             Dim ColumnID As New DataGridViewTextBoxColumn
  97.             With ColumnID
  98.                 .DataPropertyName = "ProductID"
  99.                 .Name = "ProductID"
  100.                 .HeaderText = "ProductID"
  101.                 .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
  102.             End With
  103.             .Columns.Add(ColumnID)

  104.             Dim Column1 As New DataGridViewTextBoxColumn
  105.             With Column1
  106.                 .DataPropertyName = "ProductName"
  107.                 .Name = "ProductName"
  108.                 .HeaderText = "ProductName"
  109.                 .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
  110.             End With
  111.             .Columns.Add(Column1)

  112.             Dim Column2 As New DataGridViewTextBoxColumn
  113.             With Column2
  114.                 .DataPropertyName = "UnitPrice"
  115.                 .Name = "UnitPrice"
  116.                 .HeaderText = "UnitPrice"
  117.                 .DefaultCellStyle.Format = "N2"
  118.             End With
  119.             .Columns.Add(Column2)

  120.             Dim Column3 As New DataGridViewTextBoxColumn
  121.             With Column3
  122.                 .DataPropertyName = "UnitPriceX2"
  123.                 .Name = "UnitPriceX2"
  124.                 .HeaderText = "UnitPrice x 2"
  125.                 .DefaultCellStyle.Format = "N2"
  126.             End With
  127.             .Columns.Add(Column3)

  128.             Dim Column4 As New DataGridViewTextBoxColumn
  129.             With Column4
  130.                 .DataPropertyName = "UnitsInStockUpdate"
  131.                 .Name = "UnitsInStock"
  132.                 .HeaderText = "Units In Stock"
  133.                 .DefaultCellStyle.Format = "N2"
  134.             End With
  135.             .Columns.Add(Column4)

  136.             Dim Column5 As New DataGridViewTextBoxColumn
  137.             With Column5
  138.                 .DataPropertyName = "UnitsOnOrderUpdate"
  139.                 .Name = "UnitsOnOrder"
  140.                 .HeaderText = "Units On Order"
  141.                 .DefaultCellStyle.Format = "N2"
  142.             End With
  143.             .Columns.Add(Column5)

  144.             Dim Column6 As New DataGridViewTextBoxColumn
  145.             With Column6
  146.                 .DataPropertyName = "TotalStock"
  147.                 .Name = "TotalStock"
  148.                 .HeaderText = "Total Stock"
  149.                 .DefaultCellStyle.Format = "N2"
  150.             End With
  151.             .Columns.Add(Column6)

  152.             '// For Column and string constant.
  153.             'Dim Column7 As New DataGridViewTextBoxColumn
  154.             'With Column7
  155.             '.DataPropertyName = "New Header"
  156.             '.Name = "New Header"
  157.             '.HeaderText = "New Header"
  158.             '.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
  159.             'End With
  160.             '.Columns.Add(Column7)
  161.         End With

  162.         '// Columns Alignment.
  163.         For i = 2 To 6
  164.             With DGV.Columns(i)
  165.                 .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  166.                 .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
  167.             End With
  168.         Next
  169.     End Sub

  170.     ' / --------------------------------------------------------------------------------
  171.     ' / Get my project path
  172.     ' / AppPath = C:\My Project\bin\debug
  173.     ' / Replace "\bin\debug" with ""
  174.     ' / Return : C:\My Project\
  175.     Function MyPath(AppPath As String) As String
  176.         '/ MessageBox.Show(AppPath);
  177.         AppPath = AppPath.ToLower()
  178.         '/ Return Value
  179.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  180.         '// If not found folder then put the \ (BackSlash) at the end.
  181.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  182.     End Function
  183. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

2

โพสต์

144

เครดิต

Member

Rank: 2

เครดิต
144
โพสต์ 2019-3-1 18:45:34 | ดูโพสต์ทั้งหมด

มี link download ไหมครับ ผมหาไม่เจอ

311

กระทู้

502

โพสต์

6072

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6072
 เจ้าของ| โพสต์ 2019-3-2 11:52:19 | ดูโพสต์ทั้งหมด

teerapong ตอบกลับเมื่อ 2019-3-1 18:45
มี link download ไหมครับ ผมหาไม่เจอ

ขอโทษทีครับ ไม่ได้เช็คไฟล์ ตอนนี้เรียบร้อยแล้วครับ
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

2

โพสต์

144

เครดิต

Member

Rank: 2

เครดิต
144
โพสต์ 2019-3-6 00:44:01 | ดูโพสต์ทั้งหมด

thongkorn ตอบกลับเมื่อ 2019-3-2 11:52
ขอโทษทีครับ ไม่ได้เช็คไฟล์ ตอนนี้เรียบร้อยแล้วครับ

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-5-6 01:32 , Processed in 0.181734 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้