tags:

views:

186

answers:

1

I have Windows Form application and a label which is databound based on a selection from a DataGridView. What I am trying to do is format the label text (which is filesize in bytes) after a new row in the grid is selected. How do I hook this up ? I tried using TextChanged but my formatting function is called twice, once for when the databound text is added to the label and once again AFTER I have formatted the text to what I want. I can put an if or try catch here to stop it the second time but was hoping there is a better way to do this. Thanks

A: 

Don't use the TextChanged event. Rather, get the Binding instance that is connected to the Label and attach to the Format event on the binding.

Then, when the data changes and updates the label, the Format event will fire, and it is in that event handler where you can set your format in a custom manner (which I assume is not expressible using regular format strings).

casperOne
I got it, thanks. filesizeLabel.DataBindings.Add("Text", BindSource, "FileSize"); filesizeLabel.DataBindings[0].Format += FormatFileSize;
Mark Stahler