Datagridview

ปรับปรุง : 2551-02-04 (รวมคำสั่ง)
1. Introduction to Datagridview
    1.1 use value from datagridview textbox1.text = Datagridview1.currentcell.value.tostring textbox2.text = Val(DataGridView1.CurrentRow.Index.ToString) TextBox3.Text = DataGridView1.Rows(0).Cells(0).Value TextBox4.Text = DataGridView1.Rows(0).Cells(1).Value 1.2 manage datagridview DataGridView1.Rows(0).Cells(0).Value = TextBox3.Text DataGridView1.Enabled = True DataGridView1.CurrentCell = DataGridView1.Item(0, 0) DataGridView1.CurrentRow.Selected = True DataGridView1.Focus() 1.3 load .mdb to datagridview Imports System.Data.OleDb Public Class Form1 Dim strconn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/dthai97.mdb" Dim cn As New OleDbConnection(strconn) Dim da As New OleDbDataAdapter Dim ds As New DataSet Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load da.SelectCommand = New OleDbCommand("select * from tbthai", cn) da.Fill(ds, "tb") DataGridView1.DataSource = ds.Tables("tb") End Sub End Class

2. Updating Table
    Update Table by OleDbDataAdapter in DataGridview
      Dim cb As New OleDbCommandBuilder(da) Try Dim chg As Integer = da.Update(ds, "tb") MsgBox(chg) Catch ex As Exception MsgBox(ex.Message) End Try
    Put data to datagridview from TableAdapter
      Me.TbthaiTableAdapter.Fill(Me.Dthai97DataSet.tbthai)

3. MyODBC & MyConnection

3.1 load myodbc to datagridview
Imports System.Data.Odbc
Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ConnString As String = "Dsn=modbc351mysql;Uid=admin;Pwd=p"
    Dim sql As String = "select user,password from user"
    Dim Conn As OdbcConnection = New OdbcConnection(ConnString)
    Dim Cmd As OdbcCommand = New OdbcCommand(sql, Conn)
    Dim ReaderCat As OdbcDataReader = Cmd.ExecuteReader()
    Dim s As String = ""
    Conn.Open()
    Do While ReaderCat.Read()
       s &= ">" & ReaderCat.GetString(0) & Chr(10)
    Loop
    ReaderCat.Close()
    Conn.Close()
    DataGridView1.Rows(0).Cells(0).Value = s
  End Sub
End Class
3.2 load myconnection to datagridview
Imports System.Data.Odbc
Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ConnString As String = "Dsn=modbc351mysql;Uid=admin;Pwd=p"
    Dim sql As String = "select user,password from user"
    Dim myConnection As OdbcConnection 
    myConnection = New OdbcConnection
    myConnection.ConnectionString = ConnString
    myConnection.Open()
    Dim da As New OdbcDataAdapter(sql, myConnection)
    Dim CmdB As New OdbcCommandBuilder(da)
    Dim ds As New DataSet 
    da.Fill(ds, "user") ' put user table to dataset
    Dim dv As New DataView
    dv.Table = ds.Tables("user") ' put dataset to dataview
    DataGridView1.DataSource = dv
  End Sub
End Class

4. put row by row

Put data to datagrid
Imports system.data.oledb
Public Class Form1
  Dim strdb As String = "c:\northwind.mdb"
  Dim strconn As String 
  strconn = "provider=microsoft.jet.oledb.4.0;data source=" & strdb
  Dim strsql As String
  Dim cn As New System.Data.OleDb.OleDbConnection(strconn)
  Dim cm As New OleDbCommand(strsql, cn)
  Dim dr As OleDbDataReader
  Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim x As Integer = DataGridView1.Columns.Count()
    Dim i As Integer = 0
    For i = 0 To x : DataGridView1.Columns.Clear() : Next
    strsql = "select * from employees"
    cm = New OleDbCommand(strsql, cn)
    cn.Open()
    dr = cm.ExecuteReader()
    DataGridView1.Columns.Add("eid", "eid")
    DataGridView1.Columns.Add("ename", "ename")
    Do While dr.Read()
      x = DataGridView1.Rows.Count() - 1
      DataGridView1.Rows.Add()
      DataGridView1.Rows(x).Cells(0).Value = dr.Item(0)
      DataGridView1.Rows(x).Cells(1).Value = dr.Item(1)
    Loop
    cn.Close()
  End Sub
End Class

5. resize of datagridview
(undock in parent container)

Public Class Form1
    Private Sub Form1_Load( ...
        resizeme()
    End Sub
    Sub resizeme()
        Dim gap As Integer = 30
        DataGridView1.Left = 0
        DataGridView1.Top = gap
        DataGridView1.Width = Me.Width - 6
        DataGridView1.Height = Me.Height - 25 - gap
    End Sub
    Private Sub Form1_Resize( ...
        resizeme()
    End Sub
End Class

Web Guides

+ http://www.thaiall.com/vbnet/datagridview_text.htm
+ http://www.thaiall.com/vbnet/datagridview_update.htm
+ http://www.thaiall.com/vbnet/sql_dbthai.htm
+ http://www.thaiall.com/vbnet/mysql_odbc.htm

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223