0

I'm using WPF to display Products on my DataGrid, for database I am using MySql database,

decimals in mysql are stored as 100.30, 100.10, 90.40, as you can see mysql is using dot(".") as separator between decimals, and to show it on my screen as comma separated, I used next thing:

 <DataGridTextColumn Binding="{Binding Price, StringFormat='{}{0:C}',ConverterCulture=EN}"   Header="PRICE" FontSize="15" FontFamily="Verdana" Width="10*" />

I set culture info to my DataGridTextColum where I am displaying my price, but in case someone someday would like to change that culture info how could I acces this DataGridTextColumn and change culture info for that column "Price" .. ?

Thanks guys, Cheers

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102

1 Answers1

1

If you give the column an x:Name (like "col1") in your XAML markup you should be able to access it and its properties programmatically like this:

Binding b = col1.Binding as Binding;
string format = b.StringFormat;
var c = b.ConverterCulture;

Note that you won't be able to change any property of the binding after it has been used though so if you need to change the culture or the StringFormat you must either do this in the constructor before the binding has been resolved or by simply editing the XAML markup. It can't be done programmatically after the binding has been resolved.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Can't I do it like you wrote before, like this? Binding b = col1.Binding as Binding; b.StringFormat = "N2"; b.ConverterCulture = new System.Globalization.CultureInfo("de-DE"); Like that I could store to database string StringFormat "N2" and CultureInfo "de-DE" and In case I want to change culture info for my column someday, I could change it simply in database and on Load of this form I could check for it in database and add it to binding with "col1" column name.. like string CI = GetDefaultCIFromDB(); and b.ConverterCulture = new System.Globalization.CultureInfo(CI); – Roxy'Pro Mar 21 '17 at 22:27
  • You could do this but only if you do it in the constructor of the window before the binding has been resolved. You cannot change the binding properties once it has been resolved. – mm8 Mar 21 '17 at 22:29
  • yes in public MainWindow(); and everytime I load this Window I could check for string format and for culture info from database, because I use it for only 2 datagridtextcolumns + I am not getting much data from database it won't be performance issue or smth like that to do it everytime when I open this form? – Roxy'Pro Mar 21 '17 at 22:31
  • ? what do you think about this I wrote above? it's ok? – Roxy'Pro Mar 21 '17 at 22:37
  • Yes, as long as you don't modify the binding after it has been used it is OK as far as modifying the binding properties is concerned. Whether you should connect to a database the first thing you do when each time you open a window is another story really. But I guess you maybe only open the MainWindow of your application once. – mm8 Mar 21 '17 at 22:40
  • Yes I open it only once in a while for example 10 times on a day, and it is ok to set CultureInfo and StringFormat in constructor, I think it's not performansse issue or bad practice because it's not big thing? – Roxy'Pro Mar 21 '17 at 22:53
  • Connecting to the database is probably not an issue then. If it turns out to be you could consider caching the data after you have called the database for the first time. – mm8 Mar 21 '17 at 22:56
  • so In case I'm reopening MainWindow every minute lets say, I should cache that data to avoid calling GetDefaultCIFromDB(); each time windows opens, is that correct sir? And in case I change that column/row in my database, how could I 'reset'/'update' cached values? – Roxy'Pro Mar 21 '17 at 23:00
  • so In case I'm reopening MainWindow every minute lets say, I should cache that data to avoid calling GetDefaultCIFromDB(); each time windows opens, is that correct sir? And in case I change that column/row in my database, how could I 'reset'/'update' cached values? Whatever, I think in this case where I'm getting only 2 columns value from DB each time I open this form, database accessing will not be any issue in this moment, if we are talking about hunders of row than maybe... am I right ?? :)) Its pleasure to read your answers&comments, seriously Sir – Roxy'Pro Mar 21 '17 at 23:21