Solved

DataGridView / ComboBox Column?

Posted on 2016-11-04
15
42 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 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