Solved

DataGridView / ComboBox Column?

Posted on 2016-11-04
15
50 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
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!

 
LVL 1

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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

622 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