Classe
Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class DataGridIconOnlyColumn
Inherits DataGridTextBoxColumn
Private WithEvents _icons As ImageList
Private _getIconIndex As delegateGetIconIndexForRow
Private DtsTrovaDati As DataSet
Public Sub New(ByVal Icons As ImageList, ByVal getIconIndex As delegateGetIconIndexForRow, ByVal dtsDatiConfronta As DataSet)
MyBase.New()
_icons = Icons
_getIconIndex = getIconIndex
DtsTrovaDati = dtsDatiConfronta
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Try
'erase background
Dim IntVAlore As Integer
IntVAlore = DtsTrovaDati.Tables("Dati").Rows(rowNum).Item(0)
If IntVAlore = 2 Then
g.FillRectangle(backBrush, bounds)
g.DrawImage(Me._icons.Images(_getIconIndex(rowNum)), bounds)
End If
Catch ex As System.Exception
' empty catch
End Try
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
'In questo modo rendiamo la colonna disabilitata.
If (Me.MappingName Is "Icona") Then
Return
End If
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)
End Sub
End Class
Delegato
Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Public Delegate Function delegateGetIconIndexForRow(ByVal row As Integer) As Integer
Form
Option Strict Off
Option Explicit On
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents Icons As ImageList
Dim DtsAnagrafica_new As New DataSet()
#Region " Codice generato da Progettazione Windows Form "
Public Sub New()
MyBase.New()
'Chiamata richiesta da Progettazione Windows Form.
InitializeComponent()
'Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent()
End Sub
'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti.
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
'Richiesto da Progettazione Windows Form
Private components As System.ComponentModel.IContainer
'NOTA: la procedura che segue è richiesta da Progettazione Windows Form.
'Può essere modificata in Progettazione Windows Form.
'Non modificarla nell'editor del codice.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(400, 144)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(400, 205)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
Me.Name = "Form1"
Me.Text = "Inserimento Icona datagrid"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim SqlConDati As OleDbConnection
Dim StrConnessione As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\Dati.mdb"
Dim DtaDAti As OleDbDataAdapter
Dim ComDati As OleDbCommand
Dim StrSql As String = "Select count(*) from Dati"
Dim DtsAnagrafica_new As New DataSet()
Dim IntRecord As Integer
SqlConDati = New OleDbConnection(StrConnessione)
Try
SqlConDati.Open()
'carico la risorsa
Me.Icons = New ImageList()
Dim iconName As String
iconName = "WinGuidaDatagridIcona.FACE02.ICO"
Dim strm As System.IO.Stream
strm = Me.GetType.Assembly.GetManifestResourceStream(iconName)
Dim icon As Icon
icon = New Icon(strm)
Me.Icons.Images.Add(icon.ToBitmap)
ComDati = New OleDbCommand(StrSql, SqlConDati)
StrSql = "select * from dati "
DtaDAti = New OleDbDataAdapter(StrSql, SqlConDati)
DtaDAti.Fill(DtsAnagrafica_new, "Dati")
Dim tableStyle As DataGridTableStyle
tableStyle = New DataGridTableStyle()
tableStyle.MappingName = "Dati"
DtsAnagrafica_new.Tables("Dati").Columns.Add("Icona")
'ID
Dim aColumnTextColumn As DataGridTextBoxColumn
aColumnTextColumn = New DataGridTextBoxColumn()
aColumnTextColumn.HeaderText = "ID"
aColumnTextColumn.MappingName = "ID"
'Nome
Dim DColumnTextColumn As DataGridTextBoxColumn
DColumnTextColumn = New DataGridTextBoxColumn()
DColumnTextColumn.HeaderText = "Nome"
DColumnTextColumn.MappingName = "Nome"
'icona
Dim iconColumn As DataGridIconOnlyColumn
iconColumn = New DataGridIconOnlyColumn(Me.Icons, New delegateGetIconIndexForRow(AddressOf MyGetImageIndexForRow), DtsAnagrafica_new)
iconColumn.HeaderText = ""
iconColumn.MappingName = "Icona"
iconColumn.Width = Me.Icons.Images(0).Size.Width
'Cognome
Dim EColumnTextColumn As DataGridTextBoxColumn
EColumnTextColumn = New DataGridTextBoxColumn()
EColumnTextColumn.HeaderText = "cognome"
EColumnTextColumn.MappingName = "cognome"
tableStyle.GridColumnStyles.Add(aColumnTextColumn)
tableStyle.GridColumnStyles.Add(iconColumn)
tableStyle.GridColumnStyles.Add(DColumnTextColumn)
tableStyle.GridColumnStyles.Add(EColumnTextColumn)
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(tableStyle)
DataGrid1.DataMember = "Dati"
DataGrid1.DataSource = DtsAnagrafica_new
Catch ecc As Exception
MsgBox(ecc.Message)
End Try
SqlConDati.Close()
End Sub
Public Function MyGetImageIndexForRow(ByVal row As Integer) As Integer
' Nel caso volessimo utilizzare più icone possiamo alternarle
Return (row Mod 1)
End Function
End Class
Nessun commento:
Posta un commento