简体   繁体   中英

DataGridView change display type

I have a BindingList that I am displaying in a DataGridView my issue is that the one of the properties that I am displaying is a byte[] which I think that it is interpreting it as a bitmap (from the error message).

There are two solutions that I can see

  1. What I want is to display it as a string so some sort of explicit cast?

  2. I could make a new column that holds the password converted to a string. This seems a bit kludgy and I would prefer to not do it.

Once you have control on the underlying data source class, you can add a special property used just for data binding (this way not breaking the existing code) and use attributes to control which one applies to UI.

Let assume your class is something like this

class MyClass
{
    // ....

    public byte[] Password { get; set; }
}

You can change it as follows

class MyClass
{
    // ....

    [Browsable(false)]
    public byte[] Password { get; set; }

    [DisplayName("Password")]
    public string PasswordText
    {
        get { ... }
    }
}

and will get the desired behavior in DataGridView and similar controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM