質問

Hi is it possible to format a property once it is binded in the datagridview but retain it's original value?

Example I have this item:

IList<User> a = new List<User>()
{
    new User { ID = 0, LinkCode = "ABC DEF G" },
    new User { ID = 1  LinkCode = "HIJ KLM N" },
    new User { ID = 2  LinkCode = "ABC DEF G" },
};

Then I have a datagridview with two columns that represent the two properties on user collection named "ID" and "LinkCode".

Is it possible to bind the collection but instead of showing the complete "LinkCode" it will just show a Substring of it but retaining the original value in case I want to save it back to the database, it's like the logic of "ValueMember" and "DisplayMember" where "ABC DEF G" is the value member but only "G" will be the display member.

役に立ちましたか?

解決

Add a property to the User object that returns just a substring of LinkCode

Something like:

public string FormattedLinkCode { get { return LinkCode.Last().ToString(); } }

EDIT: To add this functionality to a POCO object created by Entity Framework you could put this property in a partial class. Simply add a partial class to the same namespace in which your POCO resides.

public partial class User { 
    public string FormattedLinkCode 
     { 
        get { 
                if(string.IsNullOrEmpty(LinkCode))
                     return string.Empty;
                return LinkCode.Last().ToString(); 
            } 
     }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top