Resolved How to change currency,format of a column/cell in dynamically changing data grid

dv2020

Active member
Joined
Dec 18, 2020
Messages
30
Programming Experience
1-3
Hi All,

I'm trying to work out the best way to change a columns/cell to local currency on a dynamic data grid populated from MySQL
  1. The current data grid is populated dynamically from a MySQL query, where the headings and data can change.
  2. I would like the code, to check if the Heading of a column = "Money", then that entire column will be displayed in local currency.
I suspect this needs to be completed post the table being populated? I was originally trying to use the below, but of course as its dynamic "money" does not exist?

This does not work:
dggrid1.Columns["MONEY"]


Thanks in Advance

David
 
Solution
Hi All,

For anyone else who comes across this post. I coded this today, which worked for my DataGridView which is dynamicaly loaded from a MySQL source.

When a new column is added, this event checks the column and then based on the heading it assigns the currency, or percentage formatting. Works great.


Change Column Format on Column Added:
   private void dataGrid1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {

            if (e.Column.HeaderText.Trim().ToLower().Contains("avg price") || e.Column.HeaderText.Trim().ToLower().Contains("open value"))
            {
                e.Column.DefaultCellStyle.Format = "c1";
                e.Column.DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-AU");
            }

            if...
check if the Heading of a column = "Money", then that entire column will be displayed in local currency.
Look at what you've written there. That is basically pseudocode. How can you write "if" in your explanation of the requirement and then not have an if statement in your code? You need to think about the logic first and get a firm understanding of that in your head or, if you need to, written down. You can then write code to implement that logic specifically, rather than some vague idea of what the result should be.

If you're saying, without actually saying, that you want to check all columns in the grid then it should be fairly obvious that a loop is required. The grid has a Columns property that is a collection of columns. How convenient! Inside the loop, you write your if statement to test the condition you need to test on the current column and then perform the task you need to perform on that column. The task is setting the format of the data in the column and it should be fairly easy to find out how to do that with a simple web search. I just searched for "datagridview format column as money" and the very first result provided the answer and I'm sure several other results did too.

To think like a developer you need to:
  1. Break a problem down into smaller problems and address each one separately.
  2. Research the specific subproblems you don't already understand.
  3. Consider the required logic first and develop an algorithm that works for a manual process.
  4. Write code to implement the algorithm specifically.
  5. Debug code to determine exactly where and how its behaviour differs from your expectations.
  6. Combine solutions to subproblems into a single solution for the original problem.
In your case, finding a column with specific header text and setting the format of a column's data are completely separate problems and should be treated as such. No question should involve both. If you can't do either then you should post two separate questions: one for each problem.
 
Hello

Thanks for replying.

Ill come back with a more direct question and example. Feel free to delete this post as it useless in its current form.
 
Hi All,

For anyone else who comes across this post. I coded this today, which worked for my DataGridView which is dynamicaly loaded from a MySQL source.

When a new column is added, this event checks the column and then based on the heading it assigns the currency, or percentage formatting. Works great.


Change Column Format on Column Added:
   private void dataGrid1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {

            if (e.Column.HeaderText.Trim().ToLower().Contains("avg price") || e.Column.HeaderText.Trim().ToLower().Contains("open value"))
            {
                e.Column.DefaultCellStyle.Format = "c1";
                e.Column.DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-AU");
            }

            if (e.Column.HeaderText.Trim().ToLower().Contains("market per"))
            {
               e.Column.DefaultCellStyle.Format = "P1";
            }

        }
 
Solution

Latest posts

Back
Top Bottom