Solved

DataGridView / ComboBox Column?

Posted on 2016-11-04
15
30 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 37
Icons and Colors for Terms 3 24
C# Offline Apllication 5 53
Extracting last characters in a substring 5 20
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now