Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have searched the net about how to make a data in a combobox centered.
I found a code like this :

class Centered_ComboBox : ComboBox
{
    public Centered_ComboBox()
    {
        DrawItem += CenteredComboBox_DrawItem;
        this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        this.DropDownStyle = ComboBoxStyle.DropDownList;
    }

    public void CenteredComboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        e.DrawBackground();
        string txt = "";
        if (e.Index >= 0)
            txt = this.Items[e.Index].ToString();
        TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter);
        e.DrawFocusRectangle();
    }
}


And really it was perfect but when I use data Binding from SQL I found that all the data in the Combobox are written as: System.Data.DataRowView

Please I need help editing this code to achieve my needs,
Thanks in advance.


Best regards,
Michael Waguih
:)
Posted
Comments
Sergey Alexandrovich Kryukov 1-Mar-13 15:02pm    
This is a natural result. And not related to your centering (why by the way). I don't know how you did binding, but I know what happens. List boxes, combo boxes, etc... do simple thing: they use the result to Object.ToString to show as the item. The items themselves can be of any type.
—SA
Michael Waguih 1-Mar-13 15:07pm    
I knew and I knew that the problem is in this line but can I use to sole it ?
if (e.Index >= 0)
txt = this.Items[e.Index].ToString();
Sergey Alexandrovich Kryukov 1-Mar-13 15:10pm    
No, this is not a problem. The problem is: you should have the items of appropriate type. If it is your type, you should simply override its Object.ToString method.
—SA

1 solution

You can convert the datatable of string that you use as input to an array of strings, that should sort out the behavior.

It has nothing to do with the centering.
When this line:
C#
txt = this.Items[e.Index].ToString();

gets a DataRowView as input, then the ToString method will of course state that it is a DataRowView which is the result of it's ToString() implementation.

You can of course also override the ToString method.
You can also insert a check in the Centered_ComboBox class to check the type of the input and act accordingly - handle different types in the CenteredComboBox_DrawItem method.

Cheers,
Edo
 
Share this answer
 
Comments
Michael Waguih 4-Mar-13 15:51pm    
So please can you tell me about the code I must insert here?
Joezer BH 5-Mar-13 0:07am    
If you are talking about getting the value from the DataRowView then something like this:
DataRowView drv = (DataRowView)this.Items[e.Index];
txt = drv["MyColumnName"]; // either column name or use index - drv[0]
Michael Waguih 8-Mar-13 18:29pm    
Thank you that was what I was searching for :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900