EDN Admin
Well-known member
Hi all.
I need a WinForm showing master-detail records in a readonly grid.
Following the excellente example of reachrishick and Linda Liu below, I managed to present the data required.<span style="font-size:x-small; font-family:Consolas
<span style="font-size:x-small; font-family:Consolas
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4-8add6b4f244f http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4-8add6b4f244f
My problem is that the inner grid would have many records, and I would like to show only the topmotsts, but yet allowing the user to scroll down to see the others.
Is there any way to make the inner grid responsive to mouse or key events, in order to browse the data?
Here is the code for the nested grid, filled with sample data and sized in order to show just 2 out of 4 records present in inner grid.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Public DataGridView1 <span style="color:Blue; As <span style="color:Blue; New DataGridView
<span style="color:Blue; Friend list1 <span style="color:Blue; As <span style="color:Blue; New List(Of TestClass)
<span style="color:Blue; Public <span style="color:Blue; Sub <span style="color:Blue; New()
InitializeComponent()
SetupDataGridView()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Blue; Me.Size = <span style="color:Blue; New Size(700, 300)
createDatasource()
DataGridView1.DataSource = list1
<span style="color:Blue; For i <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To DataGridView1.Rows.<span style="color:Blue; Count - 1
DataGridView1.Rows(i).Height = 22.5 * 3 <span style="color:Green; contains three inner rows
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Friend <span style="color:Blue; Class TestClass
<span style="color:Blue; Private m_column1 <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private m_column2 <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private m_column3 <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private m_column4 <span style="color:Blue; As DataTable
<span style="color:Blue; Public <span style="color:Blue; Property Column1() <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Get
<span style="color:Blue; Return m_column1
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Integer)
m_column1 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property Column2() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return m_column2
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
m_column2 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property Column3() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return m_column3
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
m_column3 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property Column4() <span style="color:Blue; As DataTable
<span style="color:Blue; Get
<span style="color:Blue; Return m_column4
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As DataTable)
m_column4 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; Private <span style="color:Blue; Sub createDatasource()
<span style="color:Blue; Dim outerTab(,) <span style="color:Blue; As <span style="color:Blue; String = {{<span style="color:#A31515; "row0", <span style="color:#A31515; "dt0"}, {<span style="color:#A31515; "row1", <span style="color:#A31515; "dt1"}, {<span style="color:#A31515; "row2", <span style="color:#A31515; "dt3"}, {<span style="color:#A31515; "row3", <span style="color:#A31515; "dt3"}}
<span style="color:Blue; Dim innerTab(,,) <span style="color:Blue; As <span style="color:Blue; Integer = {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}, {{10, 10}, {11, 11}, {12, 12}, {13, 13}}, {{20, 20}, {21, 21}, {22, 22}, {23, 23}}, {{30, 30}, {31, 31}, {32, 32}, {33, 33}}}
<span style="color:Blue; For i = 0 <span style="color:Blue; To 3
<span style="color:Blue; Dim obj <span style="color:Blue; As <span style="color:Blue; New TestClass
obj.Column1 = i
obj.Column2 = outerTab(i, 0)
obj.Column3 = outerTab(i, 1)
<span style="color:Blue; Dim dt <span style="color:Blue; As <span style="color:Blue; New DataTable()
dt.Columns.Add(<span style="color:#A31515; "iCol1")
dt.Columns.Add(<span style="color:#A31515; "iCol2")
dt.Clear()
<span style="color:Blue; For j = 0 <span style="color:Blue; To 3
dt.Rows.Add(innerTab(i, j, 0), innerTab(i, j, 1))
<span style="color:Blue; Next
obj.Column4 = dt
list1.Add(obj)
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub SetupDataGridView()
<span style="color:Blue; Me.Controls.Add(DataGridView1)
<span style="color:Blue; With DataGridView1
.AutoGenerateColumns = <span style="color:Blue; False
.Name = <span style="color:#A31515; "DataGridView1"
.Location = <span style="color:Blue; New Point(25, 25)
.Size = <span style="color:Blue; New Size(600, 200)
.RowHeadersVisible = <span style="color:Blue; False
.BackgroundColor = SystemColors.Control
.BorderStyle = BorderStyle.None
.AllowUserToOrderColumns = <span style="color:Blue; True
.AllowUserToResizeColumns = <span style="color:Blue; True
.AllowUserToResizeRows = <span style="color:Blue; True
.MultiSelect = <span style="color:Blue; False
.StandardTab = <span style="color:Blue; True
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
.<span style="color:Blue; ReadOnly = <span style="color:Blue; True
.AllowUserToAddRows = <span style="color:Blue; False
.AllowUserToDeleteRows = <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col1 <span style="color:Blue; As <span style="color:Blue; New DataGridViewTextBoxColumn()
<span style="color:Blue; With Col1
.DataPropertyName = <span style="color:#A31515; "Column1"
.Name = <span style="color:#A31515; "Column1"
.Visible = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col2 <span style="color:Blue; As <span style="color:Blue; New DataGridViewTextBoxColumn()
<span style="color:Blue; With Col2
.DataPropertyName = <span style="color:#A31515; "Column2"
.Name = <span style="color:#A31515; "Column2"
.Visible = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col3 <span style="color:Blue; As <span style="color:Blue; New DataGridViewTextBoxColumn()
<span style="color:Blue; With Col3
.DataPropertyName = <span style="color:#A31515; "Column3"
.Name = <span style="color:#A31515; "Column3"
.HeaderText = <span style="color:#A31515; "Column3"
.Visible = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col4 <span style="color:Blue; As <span style="color:Blue; New NestedDgvColumn()
<span style="color:Blue; With Col4
.DataPropertyName = <span style="color:#A31515; "Column4"
.Name = <span style="color:#A31515; "Column4"
.HeaderText = <span style="color:#A31515; "Column4"
.Width = 250
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; With DataGridView1
.Columns.Insert(0, Col1)
.Columns.Insert(1, Col2)
.Columns.Insert(2, Col3)
.Columns.Insert(3, Col4)
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Class NestedDgvColumn
<span style="color:Blue; Inherits DataGridViewColumn
<span style="color:Blue; Public <span style="color:Blue; Sub <span style="color:Blue; New()
<span style="color:Blue; MyBase.<span style="color:Blue; New(<span style="color:Blue; New NestedDgvCell())
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Property CellTemplate() <span style="color:Blue; As DataGridViewCell
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; MyBase.CellTemplate
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As DataGridViewCell)
<span style="color:Green; Ensure that the cell used for the template is a DataGridViewCell.
<span style="color:Blue; If (value <span style="color:Blue; IsNot <span style="color:Blue; Nothing) <span style="color:Blue; AndAlso _
<span style="color:Blue; Not value.<span style="color:Blue; GetType().IsAssignableFrom(<span style="color:Blue; GetType(NestedDgvCell)) _
<span style="color:Blue; Then
<span style="color:Blue; Throw <span style="color:Blue; New InvalidCastException(<span style="color:#A31515; "Must be a NestedDgvCell")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; MyBase.CellTemplate = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; Public <span style="color:Blue; Class NestedDgvCell
<span style="color:Blue; Inherits DataGridViewCell
<span style="color:Blue; Private dgv <span style="color:Blue; As <span style="color:Blue; New DataGridView
<span style="color:Blue; Private <span style="color:Blue; Sub SetupDGVToDraw()
dgv.ScrollBars = ScrollBars.Vertical
dgv.Size = <span style="color:Blue; New Size(250, 22.5 * 3)
dgv.Enabled = <span style="color:Blue; True
<span style="color:Blue; If <span style="color:Blue; TypeOf (Value) <span style="color:Blue; Is DataTable <span style="color:Blue; Then
dgv.Columns.Clear()
<span style="color:Blue; For <span style="color:Blue; Each column <span style="color:Blue; As DataColumn <span style="color:Blue; In <span style="color:Blue; CType(Value, DataTable).Columns
dgv.Columns.Add(column.ColumnName, column.ColumnName)
<span style="color:Blue; Next
<span style="color:Blue; For <span style="color:Blue; Each datarow <span style="color:Blue; As DataRow <span style="color:Blue; In <span style="color:Blue; CType(Value, DataTable).Rows
dgv.Rows.Add(datarow.ItemArray)
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Protected <span style="color:Blue; Overrides <span style="color:Blue; Sub Paint(<span style="color:Blue; ByVal graphics <span style="color:Blue; As System.Drawing.Graphics, <span style="color:Blue; ByVal clipBounds <span style="color:Blue; As System.Drawing.Rectangle, <span style="color:Blue; ByVal cellBounds <span style="color:Blue; As System.Drawing.Rectangle, <span style="color:Blue; ByVal rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal cellState <span style="color:Blue; As System.Windows.Forms.DataGridViewElementStates, <span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal formattedValue <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal errorText <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal cellStyle <span style="color:Blue; As System.Windows.Forms.DataGridViewCellStyle, <span style="color:Blue; ByVal advancedBorderStyle <span style="color:Blue; As System.Windows.Forms.DataGridViewAdvancedBorderStyle, <span style="color:Blue; ByVal paintParts <span style="color:Blue; As System.Windows.Forms.DataGridViewPaintParts)
<span style="color:Blue; MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
graphics.FillRectangle(<span style="color:Blue; New SolidBrush(cellStyle.BackColor), cellBounds)
SetupDGVToDraw()
<span style="color:Blue; Dim abbreviation <span style="color:Blue; As <span style="color:Blue; New Bitmap(cellBounds.Width, cellBounds.Height)
dgv.DrawToBitmap(abbreviation, <span style="color:Blue; New Rectangle(0, 0, cellBounds.Width, cellBounds.Height))
graphics.DrawImage(abbreviation, cellBounds, <span style="color:Blue; New Rectangle(0, 0, abbreviation.Width, abbreviation.Height), GraphicsUnit.Pixel)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub InitializeEditingControl(<span style="color:Blue; ByVal rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal initialFormattedValue <span style="color:Blue; As <span style="color:Blue; Object, _
<span style="color:Blue; ByVal dataGridViewCellStyle <span style="color:Blue; As DataGridViewCellStyle)
<span style="color:Green; Set the value of the editing control to the current cell value.
<span style="color:Blue; MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; ReadOnly <span style="color:Blue; Property EditType() <span style="color:Blue; As Type
<span style="color:Blue; Get
<span style="color:Green; Return the type of the editing contol that DgvCell uses.
<span style="color:Blue; Return <span style="color:Blue; GetType(DgvEditingControl)
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Property ValueType() <span style="color:Blue; As System.Type
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; GetType(<span style="color:Blue; Object)
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As System.Type)
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; Class DgvEditingControl
<span style="color:Blue; Inherits DataGridView
<span style="color:Blue; Implements IDataGridViewEditingControl
<span style="color:Blue; Private dataGridViewControl <span style="color:Blue; As DataGridView
<span style="color:Blue; Private valueIsChanged <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Private rowIndexNum <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlFormattedValue() <span style="color:Blue; As <span style="color:Blue; Object _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlFormattedValue
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; Me.RowCount
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Object)
<span style="color:Green; If TypeOf value Is String Then
<span style="color:Green; Me.RowCount = Convert.ToInt32(value)
<span style="color:Green; End If
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Function GetEditingControlFormattedValue(<span style="color:Blue; ByVal context _
<span style="color:Blue; As DataGridViewDataErrorContexts) <span style="color:Blue; As <span style="color:Blue; Object _
<span style="color:Blue; Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
<span style="color:Blue; Return <span style="color:Blue; Me.RowCount
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Sub ApplyCellStyleToEditingControl(<span style="color:Blue; ByVal dataGridViewCellStyle <span style="color:Blue; As _
DataGridViewCellStyle) _
<span style="color:Blue; Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
<span style="color:Blue; Me.Font = dataGridViewCellStyle.Font
<span style="color:Blue; Me.ForeColor = dataGridViewCellStyle.ForeColor
<span style="color:Blue; Me.BackgroundColor = dataGridViewCellStyle.BackColor
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlRowIndex() <span style="color:Blue; As <span style="color:Blue; Integer _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlRowIndex
<span style="color:Blue; Get
<span style="color:Blue; Return rowIndexNum
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Integer)
rowIndexNum = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Function EditingControlWantsInputKey(<span style="color:Blue; ByVal key <span style="color:Blue; As Keys, _
<span style="color:Blue; ByVal dataGridViewWantsInputKey <span style="color:Blue; As <span style="color:Blue; Boolean) <span style="color:Blue; As <span style="color:Blue; Boolean _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlWantsInputKey
<span style="color:Green; Let the nested DataGridView handle the keys listed.
<span style="color:Blue; Select <span style="color:Blue; Case key <span style="color:Blue; And Keys.KeyCode
<span style="color:Blue; Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Enter, Keys.Escape, Keys.Tab, Keys.LButton
<span style="color:Blue; Return <span style="color:Blue; False
<span style="color:Blue; Case <span style="color:Blue; Else
<span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; Select
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Sub PrepareEditingControlForEdit(<span style="color:Blue; ByVal selectAll <span style="color:Blue; As <span style="color:Blue; Boolean) _
<span style="color:Blue; Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
<span style="color:Green; No preparation needs to be done.
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property RepositionEditingControlOnValueChange() _
<span style="color:Blue; As <span style="color:Blue; Boolean <span style="color:Blue; Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlDataGridView() <span style="color:Blue; As DataGridView _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlDataGridView
<span style="color:Blue; Get
<span style="color:Blue; Return dataGridViewControl
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As DataGridView)
dataGridViewControl = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlValueChanged() <span style="color:Blue; As <span style="color:Blue; Boolean _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlValueChanged
<span style="color:Blue; Get
<span style="color:Blue; Return valueIsChanged
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Boolean)
valueIsChanged = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property EditingControlCursor() <span style="color:Blue; As Cursor _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingPanelCursor
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; MyBase.Cursor
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
Thanks in advance for any help or ideas.
Maurizio
View the full article
I need a WinForm showing master-detail records in a readonly grid.
Following the excellente example of reachrishick and Linda Liu below, I managed to present the data required.<span style="font-size:x-small; font-family:Consolas
<span style="font-size:x-small; font-family:Consolas
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4-8add6b4f244f http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/0a63a483-5b15-40d6-afb4-8add6b4f244f
My problem is that the inner grid would have many records, and I would like to show only the topmotsts, but yet allowing the user to scroll down to see the others.
Is there any way to make the inner grid responsive to mouse or key events, in order to browse the data?
Here is the code for the nested grid, filled with sample data and sized in order to show just 2 out of 4 records present in inner grid.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Public DataGridView1 <span style="color:Blue; As <span style="color:Blue; New DataGridView
<span style="color:Blue; Friend list1 <span style="color:Blue; As <span style="color:Blue; New List(Of TestClass)
<span style="color:Blue; Public <span style="color:Blue; Sub <span style="color:Blue; New()
InitializeComponent()
SetupDataGridView()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
<span style="color:Blue; Me.Size = <span style="color:Blue; New Size(700, 300)
createDatasource()
DataGridView1.DataSource = list1
<span style="color:Blue; For i <span style="color:Blue; As <span style="color:Blue; Integer = 0 <span style="color:Blue; To DataGridView1.Rows.<span style="color:Blue; Count - 1
DataGridView1.Rows(i).Height = 22.5 * 3 <span style="color:Green; contains three inner rows
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Friend <span style="color:Blue; Class TestClass
<span style="color:Blue; Private m_column1 <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Private m_column2 <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private m_column3 <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private m_column4 <span style="color:Blue; As DataTable
<span style="color:Blue; Public <span style="color:Blue; Property Column1() <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Get
<span style="color:Blue; Return m_column1
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Integer)
m_column1 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property Column2() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return m_column2
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
m_column2 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property Column3() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return m_column3
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
m_column3 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property Column4() <span style="color:Blue; As DataTable
<span style="color:Blue; Get
<span style="color:Blue; Return m_column4
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As DataTable)
m_column4 = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; Private <span style="color:Blue; Sub createDatasource()
<span style="color:Blue; Dim outerTab(,) <span style="color:Blue; As <span style="color:Blue; String = {{<span style="color:#A31515; "row0", <span style="color:#A31515; "dt0"}, {<span style="color:#A31515; "row1", <span style="color:#A31515; "dt1"}, {<span style="color:#A31515; "row2", <span style="color:#A31515; "dt3"}, {<span style="color:#A31515; "row3", <span style="color:#A31515; "dt3"}}
<span style="color:Blue; Dim innerTab(,,) <span style="color:Blue; As <span style="color:Blue; Integer = {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}, {{10, 10}, {11, 11}, {12, 12}, {13, 13}}, {{20, 20}, {21, 21}, {22, 22}, {23, 23}}, {{30, 30}, {31, 31}, {32, 32}, {33, 33}}}
<span style="color:Blue; For i = 0 <span style="color:Blue; To 3
<span style="color:Blue; Dim obj <span style="color:Blue; As <span style="color:Blue; New TestClass
obj.Column1 = i
obj.Column2 = outerTab(i, 0)
obj.Column3 = outerTab(i, 1)
<span style="color:Blue; Dim dt <span style="color:Blue; As <span style="color:Blue; New DataTable()
dt.Columns.Add(<span style="color:#A31515; "iCol1")
dt.Columns.Add(<span style="color:#A31515; "iCol2")
dt.Clear()
<span style="color:Blue; For j = 0 <span style="color:Blue; To 3
dt.Rows.Add(innerTab(i, j, 0), innerTab(i, j, 1))
<span style="color:Blue; Next
obj.Column4 = dt
list1.Add(obj)
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Sub SetupDataGridView()
<span style="color:Blue; Me.Controls.Add(DataGridView1)
<span style="color:Blue; With DataGridView1
.AutoGenerateColumns = <span style="color:Blue; False
.Name = <span style="color:#A31515; "DataGridView1"
.Location = <span style="color:Blue; New Point(25, 25)
.Size = <span style="color:Blue; New Size(600, 200)
.RowHeadersVisible = <span style="color:Blue; False
.BackgroundColor = SystemColors.Control
.BorderStyle = BorderStyle.None
.AllowUserToOrderColumns = <span style="color:Blue; True
.AllowUserToResizeColumns = <span style="color:Blue; True
.AllowUserToResizeRows = <span style="color:Blue; True
.MultiSelect = <span style="color:Blue; False
.StandardTab = <span style="color:Blue; True
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
.<span style="color:Blue; ReadOnly = <span style="color:Blue; True
.AllowUserToAddRows = <span style="color:Blue; False
.AllowUserToDeleteRows = <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col1 <span style="color:Blue; As <span style="color:Blue; New DataGridViewTextBoxColumn()
<span style="color:Blue; With Col1
.DataPropertyName = <span style="color:#A31515; "Column1"
.Name = <span style="color:#A31515; "Column1"
.Visible = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col2 <span style="color:Blue; As <span style="color:Blue; New DataGridViewTextBoxColumn()
<span style="color:Blue; With Col2
.DataPropertyName = <span style="color:#A31515; "Column2"
.Name = <span style="color:#A31515; "Column2"
.Visible = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col3 <span style="color:Blue; As <span style="color:Blue; New DataGridViewTextBoxColumn()
<span style="color:Blue; With Col3
.DataPropertyName = <span style="color:#A31515; "Column3"
.Name = <span style="color:#A31515; "Column3"
.HeaderText = <span style="color:#A31515; "Column3"
.Visible = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; Dim Col4 <span style="color:Blue; As <span style="color:Blue; New NestedDgvColumn()
<span style="color:Blue; With Col4
.DataPropertyName = <span style="color:#A31515; "Column4"
.Name = <span style="color:#A31515; "Column4"
.HeaderText = <span style="color:#A31515; "Column4"
.Width = 250
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; With DataGridView1
.Columns.Insert(0, Col1)
.Columns.Insert(1, Col2)
.Columns.Insert(2, Col3)
.Columns.Insert(3, Col4)
<span style="color:Blue; End <span style="color:Blue; With
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Class NestedDgvColumn
<span style="color:Blue; Inherits DataGridViewColumn
<span style="color:Blue; Public <span style="color:Blue; Sub <span style="color:Blue; New()
<span style="color:Blue; MyBase.<span style="color:Blue; New(<span style="color:Blue; New NestedDgvCell())
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Property CellTemplate() <span style="color:Blue; As DataGridViewCell
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; MyBase.CellTemplate
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As DataGridViewCell)
<span style="color:Green; Ensure that the cell used for the template is a DataGridViewCell.
<span style="color:Blue; If (value <span style="color:Blue; IsNot <span style="color:Blue; Nothing) <span style="color:Blue; AndAlso _
<span style="color:Blue; Not value.<span style="color:Blue; GetType().IsAssignableFrom(<span style="color:Blue; GetType(NestedDgvCell)) _
<span style="color:Blue; Then
<span style="color:Blue; Throw <span style="color:Blue; New InvalidCastException(<span style="color:#A31515; "Must be a NestedDgvCell")
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; MyBase.CellTemplate = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; Public <span style="color:Blue; Class NestedDgvCell
<span style="color:Blue; Inherits DataGridViewCell
<span style="color:Blue; Private dgv <span style="color:Blue; As <span style="color:Blue; New DataGridView
<span style="color:Blue; Private <span style="color:Blue; Sub SetupDGVToDraw()
dgv.ScrollBars = ScrollBars.Vertical
dgv.Size = <span style="color:Blue; New Size(250, 22.5 * 3)
dgv.Enabled = <span style="color:Blue; True
<span style="color:Blue; If <span style="color:Blue; TypeOf (Value) <span style="color:Blue; Is DataTable <span style="color:Blue; Then
dgv.Columns.Clear()
<span style="color:Blue; For <span style="color:Blue; Each column <span style="color:Blue; As DataColumn <span style="color:Blue; In <span style="color:Blue; CType(Value, DataTable).Columns
dgv.Columns.Add(column.ColumnName, column.ColumnName)
<span style="color:Blue; Next
<span style="color:Blue; For <span style="color:Blue; Each datarow <span style="color:Blue; As DataRow <span style="color:Blue; In <span style="color:Blue; CType(Value, DataTable).Rows
dgv.Rows.Add(datarow.ItemArray)
<span style="color:Blue; Next
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Protected <span style="color:Blue; Overrides <span style="color:Blue; Sub Paint(<span style="color:Blue; ByVal graphics <span style="color:Blue; As System.Drawing.Graphics, <span style="color:Blue; ByVal clipBounds <span style="color:Blue; As System.Drawing.Rectangle, <span style="color:Blue; ByVal cellBounds <span style="color:Blue; As System.Drawing.Rectangle, <span style="color:Blue; ByVal rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, <span style="color:Blue; ByVal cellState <span style="color:Blue; As System.Windows.Forms.DataGridViewElementStates, <span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal formattedValue <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal errorText <span style="color:Blue; As <span style="color:Blue; String, <span style="color:Blue; ByVal cellStyle <span style="color:Blue; As System.Windows.Forms.DataGridViewCellStyle, <span style="color:Blue; ByVal advancedBorderStyle <span style="color:Blue; As System.Windows.Forms.DataGridViewAdvancedBorderStyle, <span style="color:Blue; ByVal paintParts <span style="color:Blue; As System.Windows.Forms.DataGridViewPaintParts)
<span style="color:Blue; MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
graphics.FillRectangle(<span style="color:Blue; New SolidBrush(cellStyle.BackColor), cellBounds)
SetupDGVToDraw()
<span style="color:Blue; Dim abbreviation <span style="color:Blue; As <span style="color:Blue; New Bitmap(cellBounds.Width, cellBounds.Height)
dgv.DrawToBitmap(abbreviation, <span style="color:Blue; New Rectangle(0, 0, cellBounds.Width, cellBounds.Height))
graphics.DrawImage(abbreviation, cellBounds, <span style="color:Blue; New Rectangle(0, 0, abbreviation.Width, abbreviation.Height), GraphicsUnit.Pixel)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub InitializeEditingControl(<span style="color:Blue; ByVal rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal initialFormattedValue <span style="color:Blue; As <span style="color:Blue; Object, _
<span style="color:Blue; ByVal dataGridViewCellStyle <span style="color:Blue; As DataGridViewCellStyle)
<span style="color:Green; Set the value of the editing control to the current cell value.
<span style="color:Blue; MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; ReadOnly <span style="color:Blue; Property EditType() <span style="color:Blue; As Type
<span style="color:Blue; Get
<span style="color:Green; Return the type of the editing contol that DgvCell uses.
<span style="color:Blue; Return <span style="color:Blue; GetType(DgvEditingControl)
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Property ValueType() <span style="color:Blue; As System.Type
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; GetType(<span style="color:Blue; Object)
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As System.Type)
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; Class DgvEditingControl
<span style="color:Blue; Inherits DataGridView
<span style="color:Blue; Implements IDataGridViewEditingControl
<span style="color:Blue; Private dataGridViewControl <span style="color:Blue; As DataGridView
<span style="color:Blue; Private valueIsChanged <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; False
<span style="color:Blue; Private rowIndexNum <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlFormattedValue() <span style="color:Blue; As <span style="color:Blue; Object _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlFormattedValue
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; Me.RowCount
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Object)
<span style="color:Green; If TypeOf value Is String Then
<span style="color:Green; Me.RowCount = Convert.ToInt32(value)
<span style="color:Green; End If
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Function GetEditingControlFormattedValue(<span style="color:Blue; ByVal context _
<span style="color:Blue; As DataGridViewDataErrorContexts) <span style="color:Blue; As <span style="color:Blue; Object _
<span style="color:Blue; Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
<span style="color:Blue; Return <span style="color:Blue; Me.RowCount
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Sub ApplyCellStyleToEditingControl(<span style="color:Blue; ByVal dataGridViewCellStyle <span style="color:Blue; As _
DataGridViewCellStyle) _
<span style="color:Blue; Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
<span style="color:Blue; Me.Font = dataGridViewCellStyle.Font
<span style="color:Blue; Me.ForeColor = dataGridViewCellStyle.ForeColor
<span style="color:Blue; Me.BackgroundColor = dataGridViewCellStyle.BackColor
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlRowIndex() <span style="color:Blue; As <span style="color:Blue; Integer _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlRowIndex
<span style="color:Blue; Get
<span style="color:Blue; Return rowIndexNum
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Integer)
rowIndexNum = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Function EditingControlWantsInputKey(<span style="color:Blue; ByVal key <span style="color:Blue; As Keys, _
<span style="color:Blue; ByVal dataGridViewWantsInputKey <span style="color:Blue; As <span style="color:Blue; Boolean) <span style="color:Blue; As <span style="color:Blue; Boolean _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlWantsInputKey
<span style="color:Green; Let the nested DataGridView handle the keys listed.
<span style="color:Blue; Select <span style="color:Blue; Case key <span style="color:Blue; And Keys.KeyCode
<span style="color:Blue; Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
Keys.Enter, Keys.Escape, Keys.Tab, Keys.LButton
<span style="color:Blue; Return <span style="color:Blue; False
<span style="color:Blue; Case <span style="color:Blue; Else
<span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; Select
<span style="color:Blue; End <span style="color:Blue; Function
<span style="color:Blue; Public <span style="color:Blue; Sub PrepareEditingControlForEdit(<span style="color:Blue; ByVal selectAll <span style="color:Blue; As <span style="color:Blue; Boolean) _
<span style="color:Blue; Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
<span style="color:Green; No preparation needs to be done.
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property RepositionEditingControlOnValueChange() _
<span style="color:Blue; As <span style="color:Blue; Boolean <span style="color:Blue; Implements _
IDataGridViewEditingControl.RepositionEditingControlOnValueChange
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlDataGridView() <span style="color:Blue; As DataGridView _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlDataGridView
<span style="color:Blue; Get
<span style="color:Blue; Return dataGridViewControl
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As DataGridView)
dataGridViewControl = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; Property EditingControlValueChanged() <span style="color:Blue; As <span style="color:Blue; Boolean _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingControlValueChanged
<span style="color:Blue; Get
<span style="color:Blue; Return valueIsChanged
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; Boolean)
valueIsChanged = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property EditingControlCursor() <span style="color:Blue; As Cursor _
<span style="color:Blue; Implements IDataGridViewEditingControl.EditingPanelCursor
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; MyBase.Cursor
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
Thanks in advance for any help or ideas.
Maurizio
View the full article