Question : Datagridviewcombobox ondrawitem works but has over 30 items in dropdown..

Hi Experts,

Not sure how to fix this. I have overloaded the Datagridviewcombobox to make it a multicolumn combobox. It works. I did this through the following site:

http://www.codeguru.com/csharp/csharp/cs_controls/datagrid/article.php/c13571/

However, I cannot reduce the size of the dropdown portion....

I am using the "onDrawItem".

I cannot find where you would or could reduce the number of items in the painted potion.

I have included the subroutine below.  It obtains a row and then paints the row horizontally. Thats fine... but the dropdown is too big.. see picture..

I tried to limit the number of rows actually being painted but it did this and still painted a full rectangle. See the second picture.

Not sure how I can reduce this rectangle while sitll having all the items and a scroll bar to view them all.

Simon
Code Snippet:
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:
Protected Overloads Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
 
            Dim rec As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) 'e.Bounds.Width
            Dim column As DataGridViewMultiColumnComboColumn = TryCast(ownerCell.OwningColumn, DataGridViewMultiColumnComboColumn)
            Dim valuesTbl As DataTable = column.valuesTbl
            Dim joinByField As String = column.joinFieldName
            Dim NormalText As New SolidBrush(System.Drawing.SystemColors.ControlText)
            Dim currentItem As Object = Items(e.Index)
            Dim currentText As String = GetItemText(currentItem)
 
            'If there is an item
            If (e.Index > -1) Then
 
                'first redraw the normal while background
                Dim normalBack As New SolidBrush(Color.White)          'TODO: fix to be system edit box background
 
                e.Graphics.FillRectangle(normalBack, rec)
 
                If DroppedDown AndAlso Not (Margin.Top = rec.Top) Then
 
                    Dim currentOffset As Integer = rec.Left
                    Dim col As DataColumn = valuesTbl.Columns(0)
 
                    'now find this text in current join field
                    Dim itemRows As DataRow() = valuesTbl.[Select](("[" & joinByField & "]='") + currentText & "'")
 
                    Dim HightlightedBack As New SolidBrush(System.Drawing.SystemColors.Highlight)
 
                    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
 
                        'draw selected color background
                        e.Graphics.FillRectangle(HightlightedBack, rec)
 
                    End If
 
                    Dim addBorder As Boolean = False
                    Dim currentRow As DataRow = itemRows(0)
 
                    Dim valueItem As Object
 
                    For Each dataRowItem As Object In currentRow.ItemArray
 
                        valueItem = dataRowItem
 
                        Dim value As String = dataRowItem.ToString()  'TODO: support for different types!!!
 
                        'Add the vertical boarder line here...
                        If addBorder Then
 
                            'draw dividing line
                            'currentOffset ++;
 
                            Dim gridBrush As New SolidBrush(Color.Gray)    'TODO: make the border color configurable
 
                            Dim linesNum As Long = lineWidth
 
                            While linesNum > 0
 
                                linesNum -= 1
 
                                Dim first As New Point(rec.Left + currentOffset, rec.Top)
                                Dim last As New Point(rec.Left + currentOffset, rec.Bottom)
                                e.Graphics.DrawLine(New Pen(gridBrush), first, last)
 
                                currentOffset += 1
 
                            End While
                            'currentOffset++;
                        Else
 
                            addBorder = True
 
                        End If
 
                        'measure the string that we are going to draw and cut it with wrapping if too large
                        Dim extent As SizeF = e.Graphics.MeasureString(value, e.Font)
                        Dim textRec As New Rectangle(currentOffset, rec.Y, CInt(extent.Width), rec.Height)
 
                        'now draw the relevant to this column value text
 
                        If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
 
                            'draw selected
                            Dim HightlightedText As New SolidBrush(System.Drawing.SystemColors.HighlightText)
 
                            'now redraw the backgrond in order to wrap the previous field if was too large
                            e.Graphics.FillRectangle(HightlightedBack, currentOffset, rec.Y, fixedAlignColumnSize, extent.Height)
 
                            'draw text as is
                            e.Graphics.DrawString(value, e.Font, HightlightedText, textRec)
 
                        Else
                            'now redraw the backgrond it order to wrap the previous field if was too large
                            e.Graphics.FillRectangle(normalBack, currentOffset, rec.Y, fixedAlignColumnSize, extent.Height)
 
                            'draw text as is
                            e.Graphics.DrawString(value, e.Font, NormalText, textRec)
 
                        End If
 
                        'advance the offset to the next position
                        currentOffset += fixedAlignColumnSize
 
                    Next
 
                End If
 
            End If
 
        End Sub

Answer : Datagridviewcombobox ondrawitem works but has over 30 items in dropdown..

I actually placed mine in the OnDrawItem method. I downloaded the code you referenced, but since it was c#, my test was obviously in c#. I don't imagine the language should matter. I placed the following line as the last line in the OnDrawItem method:

this.MaxDropDownItems = 2;

I imagine in VB.net that should be:

Me.MaxDropDownItems = 2

- Jim
Random Solutions  
 
programming4us programming4us