minton67
Messages postés1Date d'inscriptiondimanche 25 juin 2006StatutMembreDernière intervention 1 octobre 2007
-
1 oct. 2007 à 10:53
Gwendal3
Messages postés60Date d'inscriptionsamedi 4 mars 2006StatutMembreDernière intervention15 août 2011
-
4 oct. 2007 à 21:07
Bonjour,
Je me décide à poser ma question après plusieurs semaines de recherche sur le Net.
J'essaie de développer un petit logiciel pour gérer les inscription aux compétitions qu'organise mon club.
Pour ce faire j'ai un Datagridview lié à ma base de donnée, ça pour l'instant j'ai compris (enfin je pense) comment ça fonctionne.
La où ça coince, c'est que je voudrais avoir certaines colonnes du Datagridview sous forme "CheckBox" ou "ComboBox".
Pour chaque compétition, j'ai une bdd.
J'arrive à créer de telles colonnes en mode conception, mais comme mon Datagridview est lié à la bdd, il ajoute directement les colonnes de la bdd.
J'ai déjà contourné le problème en remplissant "manuellement" le Datagridview, mais ça me paraît être du bricolage.
Gwendal3
Messages postés60Date d'inscriptionsamedi 4 mars 2006StatutMembreDernière intervention15 août 2011 4 oct. 2007 à 21:07
Salut,
Voici la réponse:
<hr size="2" width="100%" /> Private Sub ParamdgvPermisChauffeur(Optional ByVal strWhere As String = Nothing)
' Charger les tables dans le DataSet et créer les relations
' > Charger la table PermisChauffeur
clPermisChauffeur.ChargerTs(objDataSet, ClassePermisChauffeur.lstChargerSchemas.AvecSchemas, strWhere & "ORDER BY Tbl_PermisChauffeur.NPermis ASC")
' > Charger la table TypePermis
clTypePermis.ChargerTs(objDataSet, ClasseTypePermis.lstChargerSchemas.SansSchemas)
' > Charger le DataGridView avec le Dataset
Me.dgvPermisChauffeur.AutoGenerateColumns = False
Me.dgvPermisChauffeur.DataSource = objDataSet.Tables("TblPermisChauffeur")
' >> Colonne ID_TblPermisChauffeur
Dim dgvIDTblPermisChauffeur As New DataGridViewTextBoxColumn
Dim iIDTblPermisChauffeur As Integer
iIDTblPermisChauffeur = Me.dgvPermisChauffeur.Columns.Add(dgvIDTblPermisChauffeur)
Me.dgvPermisChauffeur.Columns(iIDTblPermisChauffeur).DataPropertyName = "ID_PermisChauffeur"
Me.dgvPermisChauffeur.Columns(iIDTblPermisChauffeur).Name = "ID_PermisChauffeur"
Me.dgvPermisChauffeur.Columns(iIDTblPermisChauffeur).Visible = False
' >> Colonne Type de Permis
Dim dgvTypePermis As New DataGridViewComboBoxColumn()
dgvTypePermis.DataSource = objDataSet.Tables("TblTypePermis")
dgvTypePermis.DataPropertyName = "TypePermis"
dgvTypePermis.DisplayMember = "TypePermis"
dgvTypePermis.ValueMember = "ID_TypePermis"
dgvTypePermis.Name = "Type de Permis"
Dim iTypePermis As Integer
iTypePermis = Me.dgvPermisChauffeur.Columns.Add(dgvTypePermis)
Me.dgvPermisChauffeur.Columns(iTypePermis).DataPropertyName = "ID_TblTypePermis"
Me.dgvPermisChauffeur.Columns(iTypePermis).Name = "Type de Permis"
' >> Colonne Date d'obtention
Dim dgvDateObtention As New CalendarColumn
Dim iDateObtention As Integer
iDateObtention = Me.dgvPermisChauffeur.Columns.Add(dgvDateObtention)
Me.dgvPermisChauffeur.Columns(iDateObtention).DataPropertyName = "Date d'obtention"
Me.dgvPermisChauffeur.Columns(iDateObtention).Name = "Date d'Obtention"
' >> Colonne N° de Permis
Dim dgvNPermis As New DataGridViewTextBoxColumn
Dim iNPermis As Integer
iNPermis = Me.dgvPermisChauffeur.Columns.Add(dgvNPermis)
Me.dgvPermisChauffeur.Columns(iNPermis).DataPropertyName = "N° de Permis"
Me.dgvPermisChauffeur.Columns(iNPermis).Name = "N° de Permis"
' >> Colonne ID_TblChauffeur
Dim dgvIDTblChauffeur As New DataGridViewTextBoxColumn
Dim iIDTblChauffeur As Integer
iIDTblChauffeur = Me.dgvPermisChauffeur.Columns.Add(dgvIDTblChauffeur)
Me.dgvPermisChauffeur.Columns(iIDTblChauffeur).DataPropertyName = "ID_TblChauffeur"
Me.dgvPermisChauffeur.Columns(iIDTblChauffeur).Name = "ID_TblChauffeur"
Me.dgvPermisChauffeur.Columns(iIDTblChauffeur).Visible = False
End Sub
<hr size="2" width="100%" />Une petite explication.
J'ai dans le formulaire un DataGridView nu.
Les premières lignes chargent ma table dans un DataSet (Via une classe DAL) et les autres font le DataGridView.
+ La colonne Type de Permis est une colonne comportant un Combobox.
+ La colonne Date d'Obtention est une colonne comportant un contrôle DateTimePicker (contrôle qui n'existe pas, il faut ajouter une classe dans votre projet. Ici CalendarColumn.
Voici la classe CalendarColumn
<hr size="2" width="100%" />Imports System
Imports System.Windows.Forms
Public Class CalendarColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New CalendarCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a CalendarCell.
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(CalendarCell)) _
Then
Throw New InvalidCastException("Must be a CalendarCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class CalendarCell
Inherits DataGridViewTextBoxCell
Public Sub New()
' Use the short date format.
Me.Style.Format = "d"
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl As CalendarEditingControl = _
CType(DataGridView.EditingControl, CalendarEditingControl)
If Me.Value.ToString = "" Then
Me.Value = CType(Now, DateTime)
Else
ctl.Value = CType(Me.Value, DateTime)
End If
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that CalendarCell uses.
Return GetType(CalendarEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that CalendarCell contains.
Return GetType(DateTime)
End Get
End Property
'Public Overrides ReadOnly Property DefaultNewRowValue() As Object
'Get
' Use the current date and time as the default value.
'Return DateTime.Now
'End Get
'End Property
End Class
Class CalendarEditingControl
Inherits DateTimePicker
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
Me.Format = DateTimePickerFormat.Short
End Sub
Public Property EditingControlFormattedValue() As Object _
Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToShortDateString()
End Get
Set(ByVal value As Object)
If TypeOf value Is String Then
Me.Value = DateTime.Parse(CStr(value))
End If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context _
As DataGridViewDataErrorContexts) As Object _
Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Value.ToShortDateString()
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
DataGridViewCellStyle) _
Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Public Property EditingControlRowIndex() As Integer _
Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
Implements IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return False
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() _
As Boolean Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView _
Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean _
Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor _
Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)