VB.Net/GUI/DataGridView

Материал из VB Эксперт
Версия от 15:43, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Config the style for DataGridView

<source lang="vbnet">

Imports System.Windows.Forms Imports System.Data Imports System.Drawing Imports System.Data.SqlClient Public Class Form1

   Inherits System.Windows.Forms.Form
   Dim objConnection As New SqlConnection("server=localhost\yourDatabase;database=pubs;user id=sa;password=wrox")
   Dim objDataAdapter As New SqlDataAdapter()
   Dim objDataSet As New DataSet()
   Public Shared Sub Main()
       Application.Run(New Form1())
   End Sub
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       objDataAdapter.SelectCommand = New SqlCommand()
       objDataAdapter.SelectCommand.Connection = objConnection
       objDataAdapter.SelectCommand.rumandText = _
           "SELECT au_lname, au_fname, title, price " & _
           "FROM authors " & _
           "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
           "JOIN titles ON titleauthor.title_id = titles.title_id " & _
           "ORDER BY au_lname, au_fname"
       objDataAdapter.SelectCommand.rumandType = CommandType.Text
       objConnection.Open()
       objDataAdapter.Fill(objDataSet, "authors")
       objConnection.Close()
       grdAuthorTitles.AutoGenerateColumns = True
       grdAuthorTitles.DataSource = objDataSet
       grdAuthorTitles.DataMember = "authors"
       Dim objAlignRightCellStyle As New DataGridViewCellStyle
       objAlignRightCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
       " Declare and set the alternating rows style...
       Dim objAlternatingCellStyle As New DataGridViewCellStyle()
       objAlternatingCellStyle.BackColor = Color.WhiteSmoke
       grdAuthorTitles.AlternatingRowsDefaultCellStyle = objAlternatingCellStyle
       " Declare and set the style for currency cells ...
       Dim objCurrencyCellStyle As New DataGridViewCellStyle()
       objCurrencyCellStyle.Format = "c"
       objCurrencyCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
       " Change column names and styles using the column index
       grdAuthorTitles.Columns(0).HeaderText = "Last Name"
       grdAuthorTitles.Columns(1).HeaderText = "First Name"
       grdAuthorTitles.Columns(2).HeaderText = "Book Title"
       grdAuthorTitles.Columns(2).Width = 225
       " Change column names and styles using the column name
       grdAuthorTitles.Columns("price").HeaderCell.Value = "Retail Price"
       grdAuthorTitles.Columns("price").HeaderCell.Style = objAlignRightCellStyle
       grdAuthorTitles.Columns("price").DefaultCellStyle = objCurrencyCellStyle
       " Clean up
       objDataAdapter = Nothing
       objConnection = Nothing
       objCurrencyCellStyle = Nothing
       objAlternatingCellStyle = Nothing
       objAlignRightCellStyle = Nothing
   End Sub
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.grdAuthorTitles = New System.Windows.Forms.DataGridView
       CType(Me.grdAuthorTitles, System.ruponentModel.ISupportInitialize).BeginInit()
       Me.SuspendLayout()
       "
       "grdAuthorTitles
       "
       Me.grdAuthorTitles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
       Me.grdAuthorTitles.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
       Me.grdAuthorTitles.Location = New System.Drawing.Point(0, 0)
       Me.grdAuthorTitles.Name = "grdAuthorTitles"
       Me.grdAuthorTitles.Size = New System.Drawing.Size(592, 203)
       Me.grdAuthorTitles.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(576, 187)
       Me.Controls.Add(Me.grdAuthorTitles)
       Me.Name = "Form1"
       Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
       Me.Text = "Bound DataSet"
       CType(Me.grdAuthorTitles, System.ruponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents grdAuthorTitles As System.Windows.Forms.DataGridView

End Class


 </source>


Data Binding for DataGridView

<source lang="vbnet"> Imports System.Data.SqlClient Imports System.Windows.Forms <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Grid

   Inherits System.Windows.Forms.Form
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   Private components As System.ruponentModel.IContainer
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.gridDB = New System.Windows.Forms.DataGridView
       CType(Me.gridDB, System.ruponentModel.ISupportInitialize).BeginInit()
       Me.SuspendLayout()
       "
       Me.gridDB.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
       Me.gridDB.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
       Me.gridDB.Location = New System.Drawing.Point(10, 9)
       Me.gridDB.Name = "gridDB"
       Me.gridDB.Size = New System.Drawing.Size(481, 392)
       Me.gridDB.TabIndex = 1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(500, 410)
       Me.Controls.Add(Me.gridDB)
       Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Name = "Grid"
       Me.Text = "Grid"
       CType(Me.gridDB, System.ruponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents gridDB As System.Windows.Forms.DataGridView
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim Connect As String = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind;"
       Dim con As New SqlConnection(Connect)
       Dim SQL As String = "SELECT * FROM Products"
       Dim cmd As New SqlCommand(SQL, con)
       Dim adapter As New SqlDataAdapter(cmd)
       Dim dsNorthwind As New DataSet()
       con.Open()
       adapter.Fill(dsNorthwind, "Products")
       con.Close()
       gridDB.DataSource = dsNorthwind.Tables("Products")
   End Sub

End Class


 </source>