Solved

DataGridView / ComboBox Column?

Posted on 2016-11-04
15
40 Views
Last Modified: 2016-11-16
I'm using vb.net. I have a non bound DataGridView. I created a ComboBox column and added the values in the Items Collections property. I did this in the designer. I set the Column to be centered, and it does display it Centered. But How can I get the ComboBox List to appear centered when selecting an Item form the list?
0
Comment
Question by:Jess31
  • 8
  • 7
15 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41874929
From https://social.msdn.microsoft.com/Forums/en-US/e5d22714-7d11-468b-b119-953be31bcc0c/how-to-put-the-text-in-the-center-in-combobox-in-vb?forum=vbgeneral

attach this sub to your combo box drawItem Event
  Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    e.DrawBackground()
    Dim txt As String = ""
    If e.Index >= 0 Then txt = Me.Items(e.Index).ToString
    TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
    e.DrawFocusRectangle()
  End Sub
End Class

Open in new window

0
 

Author Comment

by:Jess31
ID: 41881219
I don't understand how would i use/attach this to my DataGridView ComboBoxColumn?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41881345
use this code to attach the event:

'attach the below to EditcontrolShowing of your datagridview.
Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
	Dim index As Integer = dgv.CurrentCell.ColumnIndex

       'if the column index is the combocolumn you want to center
	If index = FormatColumnIndex Then
              'ensure I get the combobox.
		Dim combobox = TryCast(e.Control, ComboBox)
		If combobox Is Nothing Then
			Return
		End If
              'set the draw mode.
		combobox.DrawMode = DrawMode.OwnerDrawFixed
              'clear the handler, if one existed.
                RemoveHandler combobox.DrawItem, AddressOf CenteredComboBox_DrawItem
              'add the draw handler.
		AddHandler combobox.DrawItem, AddressOf CenteredComboBox_DrawItem
	End If
End Sub

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:Jess31
ID: 41888295
on the Sub CenteredComboBox_DrawItem, I get an error on "Me.DrawItem"

Also on Items(e.Index).ToString I get an error on the me.Items
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41888471
Try the following instead:

 
   Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 
    Dim combo as ComboBox = Ctype(sender, ComboBox)
    e.DrawBackground()
    Dim txt As String = ""
    If e.Index >= 0 Then 
        txt = combo.Items(e.Index).ToString
    end if
    TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
    e.DrawFocusRectangle()
  End Sub

Open in new window

0
 

Author Comment

by:Jess31
ID: 41889780
Still not working. Does not even get to CenteredComboBox_DrawItem.
Here is the code
    Private Sub DGVGuidesInResults_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
        Handles DGVGuidesInResults.EditingControlShowing
        Try
            If DGVGuidesInResults.Columns(DGVGuidesInResults.CurrentCell.ColumnIndex).Name = "GuideSortRank" Then
                Dim combo As ComboBox = CType(e.Control, ComboBox)
                If (combo IsNot Nothing) Then

                                  '
                    RemoveHandler Combo.DrawItem, AddressOf CenteredComboBox_DrawItem
                    AddHandler Combo.DrawItem, AddressOf CenteredComboBox_DrawItem

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, "DGVGuidesInResults_EditingControlShowing")
        End Try
    End Sub
    Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 'Handles me.DrawItem

        Dim combo As ComboBox = CType(sender, ComboBox)
        e.DrawBackground()
        Dim txt As String = ""
        If e.Index >= 0 Then
            txt = combo.Items(e.Index).ToString
        End If
        TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
        e.DrawFocusRectangle()

    End Sub

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41890071
Can you confirm you're attaching the handler?  Are you able to find the combobox?
0
 

Author Comment

by:Jess31
ID: 41890148
I know that it never reaches  CenteredComboBox_DrawItem sub
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41890211
I understand that.  But in order to reach the centeredComboBox sub we need to first ensure that the handler is being added.

Can you set a break point on the AddHandler line to ensure that it's actually getting called?  If it is, try actually clicking on the dropdown to see if that will cause the event to fire.  If it's not, then we need to trace why.
0
 

Author Comment

by:Jess31
ID: 41890243
the combo works fine. And if I break on line 6 in the immediate windows I can get result for combo.selectedvalue. But how can I know if the handler is getting created or just not being called?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41890390
Set a break on this line:
AddHandler Combo.DrawItem, AddressOf CenteredComboBox_DrawItem


If you hit the break point there it's definitely being added.  If not set a break point at the top of the function and trace thru, let me know what's preventing it from getting to that line.
0
 

Author Comment

by:Jess31
ID: 41890453
yes, it reaches that line (AddHandler) but it never does enter the AddressOf sub
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 41890475
Above the add handler add the following line:

combo.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable

Need to tell it we are overriding the drawing.
0
 

Author Comment

by:Jess31
ID: 41890516
Yes, not it works.
what does the line do?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41890533
It's a flag to tell it to fire the handler.  Without it the handler doesn't fire even if you attach one.

https://msdn.microsoft.com/en-us/library/system.windows.forms.drawmode(v=vs.110).aspx
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question