iT邦幫忙

0

如何用vb.net 寫再利用text查詢DataGridView裡面的mysql欄位資料

krlotw 2011-11-22 19:44:0013452 瀏覽
  • 分享至 

  • xImage

如何用vb.net 寫再利用text查詢DataGridView裡面的mysql欄位資料,打完後只會顯示所查詢資料
請大家幫幫忙阿""
這裡研究好久還是不會
先謝謝大大們了

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

6
wiseguy
iT邦超人 1 級 ‧ 2011-11-23 09:53:55

先加裝 MySQL Connector .Net
在表單上佈置 1 個 DataGridView、1 個 TextBox、1 個 Button
程式 Form1.vb 如下:

<pre class="c" name="code">Imports MySql.Data.MySqlClient

Public Class Form1
    Dim connectionString As String = "資料庫連線 DSN"
    Dim sql As String = "SELECT * FROM *"
    Dim connection As New MySqlConnection(connectionString)
    Dim dataadapter As New MySqlDataAdapter(Sql, connection)
    Dim ds As New DataSet()

    Private Sub Fetch_Data(Filter as System.String)
        Dim dv As DataView
        dv = New DataView(ds.Tables(0), Filter, "", DataViewRowState.CurrentRows)
        DataGridView1.DataSource = dv
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        connection.Open()
        dataadapter.Fill(ds, "Titles_table")
        connection.Close()

        Fetch_Data("")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Fetch_Data(TextBox1.Text)
    End Sub
End Class

只要在 TextBox 打上過濾條件 (也就是 SQL 指令的 where 部份),點 button 就會執行查詢。

2
richardalgor
iT邦新手 5 級 ‧ 2012-05-30 01:20:52

You can extend the DataGridView control in a number of ways to build custom behaviors into your applications. http://vb.net-informations.com/datagridview/vb.net_datagridview_tutorial.htm giving you a detailed insight into datagridview.

thanks
algor

我要發表回答

立即登入回答