Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Visual Basic > 在内存中建表在DagaGrid中显示
【标  题】:在内存中建表在DagaGrid中显示
【关键字】:DagaGrid
【来  源】:BLOG.CSDN.NET

在内存中建表在DagaGrid中显示

Your Ad Here

Public Class Form1
    Inherits System.Windows.Forms.Form

    '[定义数据集]
    'Dim dsCtf As DataSet
    Dim dsCtf As New DataSet("dsCtf")
    Dim tabShowDataGrid As New DataTable("showDataGrid")   '用于显示datagrid的内存表
    Private TablesAlreadyAdded As Boolean   '是否加入了表头

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents btnCreateTable As System.Windows.Forms.Button
    Friend WithEvents btnshowTable As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.btnCreateTable = New System.Windows.Forms.Button
        Me.btnshowTable = New System.Windows.Forms.Button
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(8, 16)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(448, 136)
        Me.DataGrid1.TabIndex = 0
        '
        'btnCreateTable
        '
        Me.btnCreateTable.Location = New System.Drawing.Point(176, 168)
        Me.btnCreateTable.Name = "btnCreateTable"
        Me.btnCreateTable.Size = New System.Drawing.Size(112, 32)
        Me.btnCreateTable.TabIndex = 1
        Me.btnCreateTable.Text = "建立内存表"
        '
        'btnshowTable
        '
        Me.btnshowTable.Location = New System.Drawing.Point(176, 232)
        Me.btnshowTable.Name = "btnshowTable"
        Me.btnshowTable.Size = New System.Drawing.Size(112, 32)
        Me.btnshowTable.TabIndex = 2
        Me.btnshowTable.Text = "显示内存表"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(464, 285)
        Me.Controls.Add(Me.btnshowTable)
        Me.Controls.Add(Me.btnCreateTable)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnCreateTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTable.Click

        '建表
        createTab()

        '填充数据
        addInfo()

        '提示,此提示应该由建表与填充数据执行成功后才能显示,本例中没有
        MessageBox.Show("建立内存表成功,请点击显示进行查看")
    End Sub

    '[建立内存表]用于显示datagrid内容的表结构
    Private Sub createTab()

        If dsCtf.Tables.Contains("showDataGrid") = False Then
            dsCtf.Tables.Add(tabShowDataGrid)
        End If

        '在内存表showDataGrid中建立5个列
        Dim cCountID As New DataColumn("cCountID")         'add the countyID 
        Dim cCountName As New DataColumn("cCountName")     'add the countyName
        Dim iLastStartNO As New DataColumn("iLastStartNo") 'add the lastNo
        Dim iEndNo As New DataColumn("iEndNo")             'add the lastEndNo
        Dim iNowNo As New DataColumn("iNowNo")             'add the inowno

        '把这些列加入到表中
        If tabShowDataGrid.Columns.Contains("cCountID") = False Then
            tabShowDataGrid.Columns.Add(cCountID)
        End If
        If tabShowDataGrid.Columns.Contains("cCountName") = False Then
            tabShowDataGrid.Columns.Add(cCountName)
        End If
        If tabShowDataGrid.Columns.Contains("iLastStartNO") = False Then
            tabShowDataGrid.Columns.Add(iLastStartNO)
        End If
        If tabShowDataGrid.Columns.Contains("iEndNo") = False Then
            tabShowDataGrid.Columns.Add(iEndNo)
        End If
        If tabShowDataGrid.Columns.Contains("iNowNo") = False Then
            tabShowDataGrid.Columns.Add(iNowNo)
        End If

    End Sub

    '给内存表加入数据库,并在datagrid中显示
    Private Sub addInfo()

        '清空表中的数据
        tabShowDataGrid.Clear()

        Dim RowData As Data.DataRow
        Dim iRow As Int16   '行数

        '加入数据
        For iRow = 0 To 3

            RowData = tabShowDataGrid.NewRow

            RowData("cCountID") = iRow
            RowData("cCountName") = CStr(iRow) & " " & "cCountName"
            RowData("iLastStartNO") = iRow
            RowData("iEndNo") = iRow + 1
            RowData("iNowNo") = iRow + 2

            tabShowDataGrid.Rows.Add(RowData)
        Next

        '加入表头
        If TablesAlreadyAdded = True Then Exit Sub
        AddCustomDataTableStyle()
    End Sub

    ' 加入表头,加入datagrid显示内容的表头
    Private Sub AddCustomDataTableStyle()

        '可以通过 DataGridTableStyle 控制每个 DataTable 的网格的外观。
        '若要指定在显示来自特定 DataTable 的数据时所使用的 DataGridTableStyle,
        '请将 MappingName 设置为某 DataTable 的 TableName
        Dim ts1 As New DataGridTableStyle
        ts1.MappingName = "showDataGrid"

        '获取或设置网格中奇数行的背景色
        ts1.AlternatingBackColor = Color.LightGray

        '参看DataGridTextBoxColumn 类
        Dim TextCol As New DataGridTextBoxColumn
        'DataGridColumnStyle.MappingName 属性:获取或设置用于将列样式映射到数据成员的名称。
        TextCol.MappingName = "cCountID"
        TextCol.HeaderText = "编号"
        TextCol.Width = Len(TextCol.HeaderText) * 20
        ts1.GridColumnStyles.Add(TextCol)

        Dim TextCol1 As New DataGridTextBoxColumn
        TextCol1.MappingName = "cCountName"
        TextCol1.HeaderText = "名称"
        TextCol1.Width = Len(TextCol1.HeaderText) * 50
        ts1.GridColumnStyles.Add(TextCol1)

        Dim TextCol2 As New DataGridTextBoxColumn
        TextCol2.MappingName = "iLastStartNO"
        TextCol2.HeaderText = "上次起始号码"
        TextCol2.Width = Len(TextCol2.HeaderText) * 14
        ts1.GridColumnStyles.Add(TextCol2)

        Dim TextCol3 As New DataGridTextBoxColumn
        TextCol3.MappingName = "iEndNO"
        TextCol3.HeaderText = "上次结束号码"
        TextCol3.Width = Len(TextCol3.HeaderText) * 14
        ts1.GridColumnStyles.Add(TextCol3)

        Dim TextCol4 As New DataGridTextBoxColumn
        TextCol4.MappingName = "iNowNO"
        TextCol4.HeaderText = "本次起始号码"
        TextCol4.Width = Len(TextCol4.HeaderText) * 14
        ts1.GridColumnStyles.Add(TextCol4)

        DataGrid1.TableStyles.Add(ts1)

        TablesAlreadyAdded = True
    End Sub

    Private Sub btnshowTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnshowTable.Click

        'DataGrid.SetDataBinding 方法:在运行时设置 DataSource 和 DataMember 属性。
        DataGrid1.SetDataBinding(dsCtf, "showDataGrid")
    End Sub

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

    End Sub
End Class

VB.NET编程技巧——调用windows系统常用功能(原创文章0:【上一篇】
读写excel:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 如何用iso从硬盘安装Mandrake
  • 451 unable to exec qq (#4.3.0)
  • 详细讲解病毒的知识(4)
  • linux指令大全(1)
  • Jfreechart應用
  • 用sprintf()打印一个%号的难处
  • 批处理 1
  • [原创].NET2.0环境下的Ajax选型和应用(提供Demo源码下载)
  • MyEclipse+struts+Hibernate配置开发
  • 关于delphi利用ado连接数据库
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.