Questo sito utilizza cookies, anche di terze parti, per mostrare pubblicità e servizi in linea con il tuo account. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - utilizzare user control in una datagrdview
Forum - C# / VB.NET - utilizzare user control in una datagrdview

Avatar
nibbio1971 (Normal User)
Newbie


Messaggi: 1
Iscritto: 15/05/2012

Segnala al moderatore
Postato alle 12:18
Martedì, 15/05/2012
Buongiorno,

ambiente visual studio 2008, VB.NET
sto "discutendo" con uno user control, che funziona perfettamente quando è inserito in una form come details, ma non funziona quando è inserito in una datagrid view. Lo user control ha due caselle di testo, una per il dato e una che calcola la lunghezza del dato.
Se trascino dalla mia datasource lo user control sulla form, tutto funziona
Se uso una datagrdview e imposto il tipo colonna a al mio user control
1) non vedo le due caselle di testo, bensì una sola
2) quando entro in modifica, non riporta il valore del dato

non capisco cosa sbaglio e sto cercando ormai da giorni.

Grazie

Lorenzo

Posto il codice del designer dello user control e il codice

Codice sorgente - presumibilmente VB.NET

  1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  2. Partial Class myUserControlTest
  3.   Inherits System.Windows.Forms.UserControl
  4.  
  5.   'UserControl1 overrides dispose to clean up the component list.
  6.   <System.Diagnostics.DebuggerNonUserCode()> _
  7.   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  8.     Try
  9.       If disposing AndAlso components IsNot Nothing Then
  10.         components.Dispose()
  11.       End If
  12.     Finally
  13.       MyBase.Dispose(disposing)
  14.     End Try
  15.   End Sub
  16.  
  17.   'Required by the Windows Form Designer
  18.   Private components As System.ComponentModel.IContainer
  19.  
  20.   'NOTE: The following procedure is required by the Windows Form Designer
  21.   'It can be modified using the Windows Form Designer.  
  22.   'Do not modify it using the code editor.
  23.   <System.Diagnostics.DebuggerStepThrough()> _
  24.   Private Sub InitializeComponent()
  25.     Me.ilMioDato = New System.Windows.Forms.TextBox
  26.     Me.ilMioRisultato = New System.Windows.Forms.TextBox
  27.     Me.SuspendLayout()
  28.     '
  29.     'ilMioDato
  30.     '
  31.     Me.ilMioDato.Location = New System.Drawing.Point(0, 0)
  32.     Me.ilMioDato.Name = "ilMioDato"
  33.     Me.ilMioDato.Size = New System.Drawing.Size(185, 20)
  34.     Me.ilMioDato.TabIndex = 0
  35.     '
  36.     'ilMioRisultato
  37.     '
  38.     Me.ilMioRisultato.Enabled = False
  39.     Me.ilMioRisultato.Location = New System.Drawing.Point(191, 0)
  40.     Me.ilMioRisultato.Name = "ilMioRisultato"
  41.     Me.ilMioRisultato.Size = New System.Drawing.Size(189, 20)
  42.     Me.ilMioRisultato.TabIndex = 1
  43.     '
  44.     'myUserControlTest
  45.     '
  46.     Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
  47.     Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
  48.     Me.Controls.Add(Me.ilMioRisultato)
  49.     Me.Controls.Add(Me.ilMioDato)
  50.     Me.Name = "myUserControlTest"
  51.     Me.Size = New System.Drawing.Size(383, 22)
  52.     Me.ResumeLayout(False)
  53.     Me.PerformLayout()
  54.  
  55.   End Sub
  56.   Friend WithEvents ilMioDato As System.Windows.Forms.TextBox
  57.   Friend WithEvents ilMioRisultato As System.Windows.Forms.TextBox
  58.  
  59. End Class



Codice sorgente - presumibilmente VB.NET

  1. <System.ComponentModel.DefaultBindingProperty("Text")> _
  2. Public Class myUserControlTest
  3.  
  4.   Public Overrides Property Text() As String
  5.     Get
  6.       Return Me.ilMioDato.Text
  7.     End Get
  8.     Set(ByVal value As String)
  9.       Me.ilMioDato.Text = value
  10.     End Set
  11.   End Property
  12.  
  13.   Private Sub ilMioDato_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ilMioDato.TextChanged
  14.     Me.ilMioRisultato.Text = Me.ilMioDato.Text.Length
  15.   End Sub
  16. End Class
  17.  
  18. Public Class NDC
  19.   Inherits DataGridViewTextBoxCell
  20.   Public Overrides ReadOnly Property EditType() As System.Type
  21.     Get
  22.       Return GetType(myUserControlTest_DGVColumnType)
  23.     End Get
  24.   End Property
  25.  
  26.   Public Overrides ReadOnly Property ValueType() As System.Type
  27.     Get
  28.       Return GetType(String)
  29.     End Get
  30.   End Property
  31.  
  32. End Class
  33.  
  34. Public Class myUserControlTest_WithCalendarDGVColumn
  35.   Inherits DataGridViewColumn
  36.   Public Sub New()
  37.     MyBase.new(New NDC)
  38.   End Sub
  39.  
  40.  
  41.  
  42.   ' ---> KKKKK
  43.   Public Overrides Property CellTemplate() As DataGridViewCell
  44.     Get
  45.       Return MyBase.CellTemplate
  46.     End Get
  47.     Set(ByVal value As DataGridViewCell)
  48.  
  49.       ' Ensure that the cell used for the template is a NDC.
  50.       If (value IsNot Nothing) AndAlso _
  51.           Not value.GetType().IsAssignableFrom(GetType(NDC)) _
  52.           Then
  53.         Throw New InvalidCastException("Must be a NDC")
  54.       End If
  55.       MyBase.CellTemplate = value
  56.  
  57.     End Set
  58.   End Property
  59.   ' <--- KKKKK
  60.  
  61. End Class
  62.  
  63.  
  64. Class myUserControlTest_DGVColumnType
  65.   'this is the new class to get your usercontrol into the datagridcolumn class
  66.   'here your have to change only the first line i.e inherits tbnumber to inherit 'your user control. Remianing all codes are for datagridview to track the keys 'and values. you need not change anything
  67.  
  68.   Inherits myUserControlTest
  69.  
  70.  
  71.   ' (this usercontrol is for getting numeric input only)
  72.   ' You can get this code for this class in my other thread
  73.  
  74.   ' here after you need not change any code
  75.  
  76.   Implements IDataGridViewEditingControl
  77.  
  78.   Private _dataGridView As DataGridView
  79.   Private _rowIndex As Integer
  80.   Private _valueChanged As Boolean
  81.  
  82.   Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
  83.       Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
  84.  
  85.     Me.BackColor = dataGridViewCellStyle.BackColor
  86.     Me.Font = dataGridViewCellStyle.Font
  87.     Me.ForeColor = dataGridViewCellStyle.ForeColor
  88.   End Sub
  89.  
  90.   Public Property EditingControlDataGridView() As DataGridView _
  91.       Implements IDataGridViewEditingControl.EditingControlDataGridView
  92.     Get
  93.       Return _dataGridView
  94.     End Get
  95.     Set(ByVal value As DataGridView)
  96.       _dataGridView = value
  97.     End Set
  98.   End Property
  99.  
  100.   Public Property EditingControlFormattedValue() As Object _
  101.       Implements IDataGridViewEditingControl.EditingControlFormattedValue
  102.     Get
  103.       Return Text
  104.     End Get
  105.     Set(ByVal value As Object)
  106.       Text = value.ToString()
  107.     End Set
  108.   End Property
  109.  
  110.  
  111.   Public Property EditingControlRowIndex() As Integer _
  112.       Implements IDataGridViewEditingControl.EditingControlRowIndex
  113.     Get
  114.       Return _rowIndex
  115.     End Get
  116.     Set(ByVal value As Integer)
  117.       _rowIndex = value
  118.     End Set
  119.   End Property
  120.  
  121.   Public Property EditingControlValueChanged() As Boolean _
  122.       Implements IDataGridViewEditingControl.EditingControlValueChanged
  123.     Get
  124.       Return _valueChanged
  125.     End Get
  126.     Set(ByVal value As Boolean)
  127.       _valueChanged = value
  128.     End Set
  129.   End Property
  130.  
  131.  
  132.   Public Function EditingControlWantsInputKey(ByVal keyData As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
  133.       Implements IDataGridViewEditingControl.EditingControlWantsInputKey
  134.  
  135.     Return True
  136.   End Function
  137.  
  138.   Public ReadOnly Property EditingPanelCursor() As Cursor _
  139.       Implements IDataGridViewEditingControl.EditingPanelCursor
  140.     Get
  141.       Return MyBase.Cursor
  142.     End Get
  143.   End Property
  144.  
  145.  
  146.   Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object _
  147.       Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
  148.  
  149.     Return Text
  150.   End Function
  151.  
  152.   Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
  153.       Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
  154.   End Sub
  155.  
  156.  
  157.   Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean _
  158.       Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
  159.     Get
  160.       Return False
  161.     End Get
  162.   End Property
  163.  
  164.   Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
  165.     MyBase.OnTextChanged(e)
  166.     _valueChanged = True
  167.     _dataGridView.NotifyCurrentCellDirty(True)
  168.   End Sub
  169.  
  170. End Class


PM Quote