Friday, August 29, 2014

vb.net (Assign linq to array)

Imports System
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.Common
Public Class UserList
Dim odt As New DataTable
Dim ods As New DataSet
Dim arr As Array
Public Sub UserList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
Form1.connect()
Form1.conn.Open()
Dim comm3 As OdbcCommand
comm3 = New OdbcCommand("SELECT * FROM `tbl_user`", Form1.conn)
Dim myDA As OdbcDataAdapter = New OdbcDataAdapter(comm3)
odt.Columns.Add("User ID", GetType(Int32))
odt.Columns.Add("First Name", GetType(String))
odt.Columns.Add("Last Name", GetType(String))
myDA.Fill(ods, "tbl_user")
myDA.Dispose()
'odt = ods.Tables(0)
Dim users As DataTable = ods.Tables("tbl_user")
ods.Dispose()
'Dim query = From user In users.AsEnumerable()
Dim query = From user In users
Select user
For Each u In query
' MsgBox(u.Field(Of String)("first_name"))
odt.Rows.Add(u.Field(Of Integer)("user_id"), u.Field(Of String)("first_name"), u.Field(Of String)("last_name"))
Next
'''''''''''''
arr = query.ToArray
'odt = ods.Tables(0)
dgView.DataSource = odt
dgView.AllowUserToAddRows = False
dgView.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgView.Columns(0).HeaderText = "User ID"
dgView.Columns(1).HeaderText = "First Name"
Dim columnHeaderStyle As New DataGridViewCellStyle()
columnHeaderStyle.BackColor = Color.Beige
columnHeaderStyle.Font = New Font("Tahoma", 10, FontStyle.Bold)
dgView.ColumnHeadersDefaultCellStyle = columnHeaderStyle
Me.dgView.DefaultCellStyle.Font = New Font("Tahoma", 10)
dgView.ReadOnly = True
Form1.conn.Close()
End Sub
Private Sub ButtonClose_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Me.Close()
End Sub
Private Sub TextBoxsearch_TextChanged(sender As Object, e As EventArgs) Handles TextBoxsearch.TextChanged
Form1.connect()
Form1.conn.Open()
Dim comm3 As OdbcCommand
comm3 = New OdbcCommand("SELECT * FROM `tbl_user` where username like ?", Form1.conn)
comm3.Parameters.AddWithValue("username", "%" & TextBoxsearch.Text & "%")
Dim myDA As OdbcDataAdapter = New OdbcDataAdapter(comm3)
odt.Clear()
myDA.Fill(ods, "tbl_user")
myDA.Dispose()
odt = ods.Tables("tbl_user")
dgView.DataSource = odt
End Sub
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
FormUser.Show()
Dim dt As New DataTable
dt.Columns.Add("User ID", GetType(Int32))
dt.Columns.Add("First Name", GetType(String))
dt.Columns.Add("Last Name", GetType(String))
For Each u In arr
dt.Rows.Add(CInt(u(0)), u(1).ToString, u(2).ToString)
Next
DataGridView1.DataSource = dt
End Sub
End Class

No comments:

Post a Comment