Datagridview - combobox   zodpovězená otázka

VB.NET, WinForms

Dobrý den, zná někdo z Vás způsob, jak změnit typ buňky(ne však celého sloupce) z DataGridViewTextBoxCell na DataGridViewComboboxCell. Jde o poměrně složitý plánovací formulář a potřebuji několik prvních řádků ponechat jako TextBox a od cca 5-tého řádku potřebuji v prvním sloupci změnit typ buněk na combobox. Jak na to? Díky

nahlásit spamnahlásit spam 1 / 1 odpovědětodpovědět

To nelze udělat. Buňky v jednom sloupci musí být vždy stejného typu.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Tak to jsem si přesně myslel. Ale i tak, děkuji za odpověď.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Je to jen úvaha, ale nešlo by využít vlastnost

DataGridViewComboBoxColumn.DisplayStyle=DataGridViewComboBoxDisplayStyle.Nothing a v události Datagridview_EditingControlShowing nastavit dle potřeby ComboBox.DropDownStyle=ComboBoxStyle.Simple a ještě možná vlastnost DisplayStyleForCurrentCellOnly.

nahlásit spamnahlásit spam 0 odpovědětodpovědět

Na motivy kdesi staženého příkladu předkládám

následující pokus přidat do formu datagridview

s prvním textovým sloupcem, druhým combosloupcem

od pátého do osmého řádku dropdown nebo dále jen

dropdownlist a třetím calendarsloupcem, který byl

součástí původního příkladu,jenž byl pak následně

takto doprzněn a zbastlen.

Pokus by mohl oslovit zakladatele příspěvku.

Nový projekt a do Form1 nakopírovat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
Imports System.Windows.Forms
 
Public Class Form1
    Dim WithEvents dgv As DataGridView
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        dgv = New DataGridView
        dgv.Dock = DockStyle.Fill
        Dim ct As System.Windows.Forms.DataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn
        ct.HeaderText = "A"
        dgv.Columns.Add(ct)
        Dim cb As ComboBoxColumn = New ComboBoxColumn
        cb.HeaderText = "B"
        dgv.Columns.Add(cb)
        Dim cc As CalendarColumn = New CalendarColumn
        cc.HeaderText = "C"
        dgv.Columns.Add(cc)
        dgv.Rows.Add(10)
        Me.Controls.Add(dgv)
    End Sub
 
    Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
        If (TypeOf e.Control Is ComboBoxEditingControl) Then
            Dim cmb As ComboBoxEditingControl = CType(e.Control, ComboBoxEditingControl)
            If Not cmb Is Nothing Then
                If dgv.CurrentRow.Index < 5 Then
                    cmb.DropDownStyle = ComboBoxStyle.Simple
                ElseIf dgv.CurrentRow.Index > 7 Then
                    cmb.DropDownStyle = ComboBoxStyle.DropDownList
                Else
                    cmb.DropDownStyle = ComboBoxStyle.DropDown
                End If
            End If
        End If
    End Sub
End Class
 
Public Class ComboBoxColumn
    Inherits DataGridViewColumn
 
    Public Sub New()
        MyBase.New(New ComboBoxCell())
    End Sub
 
    Public Overrides Property CellTemplate() As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set(ByVal value As DataGridViewCell)
            If Not (value Is Nothing) AndAlso _
                Not value.GetType().IsAssignableFrom(GetType(ComboBoxCell)) _
                Then
                Throw New InvalidCastException("Must be a ComboBoxCell")
            End If
            MyBase.CellTemplate = value
        End Set
    End Property
End Class
 
Public Class ComboBoxCell
    Inherits DataGridViewTextBoxCell
 
    Public Sub New()
        '
    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 ComboBoxEditingControl = _
            CType(DataGridView.EditingControl, ComboBoxEditingControl)
        ctl.Text = Me.Value
        ctl.DropDownStyle = ComboBoxStyle.DropDown
    End Sub
 
    Public Overrides ReadOnly Property EditType() As Type
        Get
            ' Return the type of the editing contol that CalendarCell uses.
            Return GetType(ComboBoxEditingControl)
        End Get
    End Property
 
    Public Overrides ReadOnly Property ValueType() As Type
        Get
            ' Return the type of the value that ComboxBoxCell contains.
            Return GetType(String)
        End Get
    End Property
 
    Public Overrides ReadOnly Property DefaultNewRowValue() As Object
        Get
            ' Use the current date and time as the default value.
            Return ""
        End Get
    End Property
 
End Class
Class ComboBoxEditingControl
    Inherits ComboBox
    Implements IDataGridViewEditingControl
 
    Private dataGridViewControl As DataGridView
    Private valueIsChanged As Boolean = False
    Private rowIndexNum As Integer
 
    Public Sub New()
        Me.DropDownStyle = ComboBoxStyle.DropDownList
        Me.Items.AddRange(New String() _
                    {"1", "2", "3", "4", "5", "6"})
    End Sub
 
    Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue
        Get
            Return Me.Text
        End Get
        Set(ByVal value As Object)
            Me.Text = value
        End Set
    End Property
 
    Public Function GetEditingControlFormattedValue(ByVal context _
            As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
 
        Return Me.Text
 
    End Function
    Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
            DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
 
        Me.Font = dataGridViewCellStyle.Font
        Me.ForeColor = dataGridViewCellStyle.ForeColor
        Me.BackColor = dataGridViewCellStyle.BackColor
    End Sub
 
    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 ĆomboBox 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 OnTextChanged(ByVal e As EventArgs)
        valueIsChanged = True
        Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
        MyBase.OnTextChanged(e)
    End Sub
 
    Protected Overrides Sub OnSelectedValueChanged(ByVal e As EventArgs)
        valueIsChanged = True
        Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
        MyBase.OnSelectedValueChanged(e)
    End Sub
 
End Class
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 Not (value Is 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)
        ctl.Value = IIf(Me.Value Is Nothing, ctl.Value, CType(Me.Value, DateTime))
    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
 
        Me.Font = dataGridViewCellStyle.Font
        Me.CalendarForeColor = dataGridViewCellStyle.ForeColor
        Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor
 
    End Sub
 
    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)
    End Sub
End Class

Je to šité horkou jehlou, snad to bude fungovat.

Mnoho zdaru!

nahlásit spamnahlásit spam 1 / 1 odpovědětodpovědět

Pro doplnění původní příklad s DateTimePicker:

How to: Host Controls in Windows Forms DataGridView Cells

http://msdn.microsoft.com/en-us/library/...

nahlásit spamnahlásit spam 1 / 1 odpovědětodpovědět

Odskúšal som to a funguje.

nahlásit spamnahlásit spam 1 / 1 odpovědětodpovědět

Dobrý den, chtěl bych Vám poděkovat za tento příspěvěk. Přesně tohle jsem potřeboval. Díky moc

nahlásit spamnahlásit spam 0 odpovědětodpovědět

přihlásit pomocí externího účtu

přihlásit pomocí jména a hesla

Uživatel:
Heslo:

zapomenuté heslo

 

založit nový uživatelský účet

zaregistrujte se

 
zavřít

Nahlásit spam

Opravdu chcete tento příspěvek nahlásit pro porušování pravidel fóra?

Nahlásit Zrušit

Chyba

zavřít

feedback