Imports System.Data
Imports System.Data.SqlClient
Public Class testsel
Public _showlistpro1 As String
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
Dim cnamecus As String
Public namecus As String
Private _IsSelectAllChecked As Boolean
Public Property _cnamecus() As String
Get
Return cnamecus
End Get
Set(ByVal value As String)
cnamecus = value
End Set
End Property
Private Sub testsel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PopulateData()
End Sub
Private Sub PopulateData()
'*** BindData From Database **'
Dim objConn As SqlConnection
Dim dtAdapter As SqlDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Server=192.168.0.141;Database=Productivity;user id=sa;password=P@$$w0rd"
objConn = New SqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT fgcusname,fgpro,fgname,fgcost,fgunit,fgper from PTfg order by fgper ASC"
'If strKeyWord <> "" Then
' strSQL = strSQL & " AND Name Like '%" & strKeyWord & "%' "
'End If
dtAdapter = New SqlDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.CodeproductDataGridView.DataSource = dt
'******************************'
Me.CodeproductDataGridView.Columns.Clear()
With CodeproductDataGridView.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font("Microsoft Sans Serif", 9, FontStyle.Regular)
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
'*** Column (Select) ***'
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.CodeproductDataGridView.Columns.Add(ColumnCheckBox)
'*** Checkbox in Header ***'
Dim ckBox As New CheckBox()
Dim rect As Rectangle = CodeproductDataGridView.GetCellDisplayRectangle(0, -1, True)
ckBox.Size = New Size(18, 18)
rect.X = rect.Location.X + (rect.Width / 4)
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, New EventHandler(AddressOf ckBox_CheckedChanged)
Me.CodeproductDataGridView.Controls.Add(ckBox)
CodeproductDataGridView.Columns(0).Frozen = False
'*** Column (CustomerID) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "fgcusname"
ColumnTextBox.HeaderText = "fgcusname"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.CodeproductDataGridView.Columns.Add(ColumnTextBox)
'*** Column (Name) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "fgpro"
ColumnTextBox.HeaderText = "fgpro"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.CodeproductDataGridView.Columns.Add(ColumnTextBox)
'*** Column (Email) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "fgname"
ColumnTextBox.HeaderText = "fgname"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.CodeproductDataGridView.Columns.Add(ColumnTextBox)
'*** Column (CountryCode) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "fgcost"
ColumnTextBox.HeaderText = "fgcost"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.CodeproductDataGridView.Columns.Add(ColumnTextBox)
'*** Column (Budget) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "fgunit"
ColumnTextBox.HeaderText = "fgunit"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.CodeproductDataGridView.Columns.Add(ColumnTextBox)
'*** Column (Used) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "fgper"
ColumnTextBox.HeaderText = "fgper"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.CodeproductDataGridView.Columns.Add(ColumnTextBox)
End Sub
Private Sub ckBox_CheckedChanged()
Dim i As Integer = 0
For j As Integer = 0 To Me.CodeproductDataGridView.RowCount - 1
Me.CodeproductDataGridView(0, j).Value = True
Next
End Sub
Public showlistpro1 As New List(Of String)
Private Sub CodeproductDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CodeproductDataGridView.CellContentClick
'PopulateData()
'ckBox_CheckedChanged()
Dim gr As DataGridView = CodeproductDataGridView
For index As Integer = 0 To CodeproductDataGridView.Rows.Count - 1
If (gr.Rows(index).Cells(0).Value <> Nothing) Then 'checked
' MessageBox.Show("no " + gr.Rows(index).Cells(1).Value.ToString())
Else
'MessageBox.Show("yes " + gr.Rows(index).Cells(1).Value.ToString())
End If
'MessageBox.Show("Rows " + dr.Index.ToString() + " selected")
Next
End Sub
Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
Dim gr As DataGridView = CodeproductDataGridView
For index As Integer = 0 To CodeproductDataGridView.Rows.Count - 1
If (gr.Rows(index).Cells(0).Value <> Nothing) Then 'checked
Public Class Form1
Public Item As List(Of Integer) 'ประกาศแบบ Public เปลี่ยน Integer เป็น String หรือ อย่างอื่น
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For index As Integer = 0 To DataGridView1.RowCount - 1
If (DataGridView1(0, index).Value <> Nothing) Then 'checked
Item.Add(Integer.Parse(DataGridView1(0, index).Value)) ' Item.Add
End If
Next
Me.Close() ' ส่งค่ากลับ
End Sub
End Class
Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
For index As Integer = 0 To CodeproductDataGridView.RowCount - 1
If (CodeproductDataGridView(0, index).Value <> Nothing) Then 'checked
Item.Add(CodeproductDataGridView(1, index).Value.ToString) ' Item.Add
Item.Add(CodeproductDataGridView(2, index).Value.ToString) ' Item.Add
End If
Next
Me.Close() ' ส่งค่ากลับ
End Sub
Public Sub btnpro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpro.Click
'Try
If txtcusid.Text = "" Then
MessageBox.Show("กรุณาเลือกรหัสลูกค้าก่อนค่ะ", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtcusid.SelectAll()
txtcusid.Focus()
Exit Sub
Else
Dim frm As New frmcodepro
frm.ShowDialog()
If frmcodepro.Item.Count > 0 Then
'For Each row As DataGridViewRow In frmcodepro.Item
'Next
End If
End If
' Catch ex As Exception
' End Try
End Sub
Public Class _Value
Public Key As String
Public Name As String
End Class
Public Class Form1
Public Item As List(Of _Value) 'ประกาศแบบ Public เปลี่ยน Integer เป็น String หรือ อย่างอื่น
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For index As Integer = 0 To DataGridView1.RowCount - 1
If (DataGridView1(0, index).Value <> Nothing) Then 'checked
Item.Add(New _Value() With {.Key = DataGridView1(1, index).Value, .Name = DataGridView1(2, index).Value}) ' Item.Add
End If
Next
Me.Close() ' ส่งค่ากลับ
End Sub
End Class
ใช้งาน
Code (VB.NET)
For Each _item As _Value In Item
MessageBox.Show(_item.Key & " " & _item.Name)
Next