| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 实例学习笔记 >> 实例分析正文
最新推荐 更多内容
DataComboBox(VB2010实例)
DataComboBox(VB2010实例)
作者:Akyao    来源:乐博网收集     更新时间:2011-2-27

本文演示如何将数据绑定到 ComboBox 控件和 DataGridView 控件,来自乐博网。

如果你想下载本文的源代码RAR压缩集合包  请访问
VB2010源代码集合包(芋头糕)    http://www.lob.cn/code/utility/2795.shtml
特别感谢网友 芋头糕 将此资源提供乐博网分享

DataComboBox实例代码如下:

' Copyright (c) Microsoft Corporation. All rights reserved.
Imports System.Data.SqlClient
Imports System.Data

Public Class MainForm

    Protected dvProducts As DataView

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadData()
    End Sub

    ''' <summary>
    ''' Display a status message saying that we're attempting to connect to SQL Server.
    ''' This only needs to be done the very first time a connection is
    ''' attempted.  After we've determined that SQL Express or SQL Server is
    ''' installed, this message no longer needs to be displayed.
    ''' </summary>
    Private Sub LoadData()

        Dim frmStatusMessage As New Status()

        frmStatusMessage.Show("Connecting to SQL Server")

        Try
            Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet.Products)

            ' Create the dataview; use a constructor to specify
            ' the sort, filter criteria for performance purposes
            dvProducts = New DataView(Me.NorthwindDataSet.Products, "", "ProductName ASC", DataViewRowState.OriginalRows)
        Catch ex As Exception
            ' Unable to connect to SQL Server or SQL Express
            frmStatusMessage.Close()
            Dim strMessage As String = "To run this sample, you must have SQL " & _
            "or SQL Express with the Northwind database installed.  " & _
            "To change the connection string, open the Settings Designer " & _
            "by double clicking on My Project in the Solution Explorer, " & _
            "select the Settings tab and change the value for the NorthwindConnectionString. " & vbCrLf & _
            " For instructions on installing SQL Express view ReadMe."
            MessageBox.Show(strMessage, "Bind Data to a ComboBox", MessageBoxButtons.OK, MessageBoxIcon.Information)
            ' Quit the program; could not connect to either SQL Server
            Application.Exit()
        End Try

        frmStatusMessage.Close()
    End Sub

    ''' <summary>
    ''' Bind to a simple array of string entries for colors.
    ''' </summary>
    Private Sub btnArray_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnArray.Click
        Dim myColors() As String = {"AQUA", "BLACK", "BLUE", "GREEN", "RED", "WHITE", "YELLOW"}

        ComboBox1.DataSource = myColors
        ComboBox1.SelectedIndex = 0
        lblDataSource.Text = "Array"
    End Sub

    ''' <summary>  更多.net源代码 来自乐博网
    ''' Bind to a simple arraylist that has entries for different shapes.
    ''' </summary>
    Private Sub btnArrayList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnArrayList.Click
        Dim myShapes As New ArrayList
        With myShapes
            .Add("Circle")
            .Add("Octagon")
            .Add("Rectangle")
            .Add("Squre")
            .Add("Trapezoid")
            .Add("Triange")
        End With

        ComboBox1.DataSource = myShapes
        ComboBox1.SelectedIndex = 0
        lblDataSource.Text = "ArrayList"
    End Sub

    ''' <summary>
    ''' Bind to an arraylist that contains entries based on the structure
    ''' that has been defined for sales divisions.
    ''' </summary>
    Private Sub btnArrayListA_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnArrayListA.Click

        Dim myDivisions As New ArrayList

        ' Add division structure entries to the arraylist
        With myDivisions
            .Add(New Divisions("CENTRAL", 1))
            .Add(New Divisions("EAST", 2))
            .Add(New Divisions("NORTH", 3))
            .Add(New Divisions("SOUTH", 4))
        End With

        ' Bind the arraylist to the combo box
        With ComboBox1
            .DataSource = myDivisions
            .DisplayMember = "Name"
            .ValueMember = "Id"
        End With

        ComboBox1.SelectedIndex = 0
        lblDataSource.Text = "ArrayList - Advanced"
        lblAssocValue.Text = ComboBox1.SelectedValue.ToString
    End Sub

    ''' <summary>
    ''' Bind to the products table from the Northwind database that has
    ''' previously been loaded into the Northwind DataSet.
    ''' Note that the table has not been sorted in any particular order.
    ''' </summary>
    Private Sub btnDS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDS.Click
        With ComboBox1
            .DataSource = Me.NorthwindDataSet.Products
            .DisplayMember = "ProductName"
            .ValueMember = "ProductID"
        End With

        ComboBox1.SelectedIndex = 0
        lblDataSource.Text = "DataSet"
        lblAssocValue.Text = ComboBox1.SelectedValue.ToString
    End Sub

    Private Sub btnDV_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDV.Click
        ' Bind to the sorted view of the products table
        'on the product name column, ascending. 
        With ComboBox1
            .DataSource = dvProducts
            .DisplayMember = "ProductName"
            .ValueMember = "ProductID"
        End With

        ComboBox1.SelectedIndex = 0
        lblDataSource.Text = "DataView"
        lblAssocValue.Text = ComboBox1.SelectedValue.ToString
    End Sub

    ''' <summary>
    ''' Bind to the BindingSource that binds to the NorthwindDataset Products table.
    ''' </summary>
    Private Sub btnDC_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDC.Click
        With ComboBox1
            .DataSource = Me.ProductsBindingSource
            .DisplayMember = "ProductName"
            .ValueMember = "ProductID"
        End With

        ComboBox1.SelectedIndex = 0
        lblDataSource.Text = "BindingSource"
        lblAssocValue.Text = ComboBox1.SelectedValue.ToString
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ' Display the associated value for the item selected from the combox,
        ' if one is available. To determine a value is available
        ' check the visibility of the groupbox. This attribute has been set to false
        ' during binding by code if a value is not available, to true if it is.
        If Me.ComboBox1.SelectedIndex >= 0 Then
            lblAssocValue.Text = ComboBox1.SelectedValue.ToString
        Else
            lblAssocValue.Text = "Nothing selected"
        End If
    End Sub

    Private Sub exitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ProductsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.ProductsBindingSource.EndEdit()
        Me.ProductsTableAdapter.Update(Me.NorthwindDataSet.Products)
    End Sub
End Class

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    Tablet PC 2005 的区分上下文功能(VB2010实例)
    响应数字化仪触笔背面的橡皮擦(VB2010实例)
    WMI编程实例(VB2010实例)
    系统服务管理实例(VB2010实例)
    进程管理(VB2010实例)
    显示进程组成模块(VB2010实例)
    任务管理器编程(VB2010实例)
    性能计数器编程(VB2010实例)
    消息队列MSMQListener(VB2010实例)
    消息队列MSMQClient(VB2010实例)
    写入事件日志(VB2010实例)
    读取事件日志(VB2010实例)
    创建和删除事件日志(VB2010实例)
    COMPort编程(VB2010实例)
    读取大文件(VB2010实例)
    读取文件(VB2010实例)

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]