0

I've tried to catch ColumnDefaultCellStyleChanged event but is not fired when DefaultCellStyle.Format is changed

i read this https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.columndefaultcellstylechanged?view=netframework-4.8 many times but i still dont know what i am doing wrong

Please some suggestions,

(I don't like to use CellFormatting Event like option)

My code

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public class Form1 : Form
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public DataTable dt;
        public DataGridView dgv;
        public Button button1, button2;

        public Form1()
        {
            dt = new DataTable();
            dt.Columns.Add("Int", typeof(int));
            dt.Columns.Add("Decimal", typeof(decimal));
            dt.Rows.Add(1, 1.1);
            dt.Rows.Add(1, 1.123);

            dgv = new DataGridView();
            dgv.Size = new Size(250, 200);
            dgv.DataSource = dt;
            this.Controls.Add(dgv);
            dgv.ColumnDefaultCellStyleChanged += new DataGridViewColumnEventHandler(dgv_ColumnDefaultCellStyleChanged);

            button1 = new Button();
            button1.Size = new Size(100, 25);
            button1.Location = new Point(10, 220);
            button1.Text = "Decimals = 2";
            this.Controls.Add(button1);
            button1.Click += new EventHandler(button1_Click);

            button2 = new Button();
            button2.Size = new Size(100, 25);
            button2.Location = new Point(150, 220);
            button2.Text = "Decimals = 4";
            this.Controls.Add(button2);
            button2.Click += new EventHandler(button2_Click);
        }

        private void dgv_ColumnDefaultCellStyleChanged(object sender, DataGridViewColumnEventArgs e)
        {
            MessageBox.Show("ColumnDefaultCellStyleChanged()");
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            dgv.Columns[1].DefaultCellStyle.Format = "N2";   
        }

        private void button2_Click(object sender, EventArgs e)
        {         
            dgv.Columns[1].DefaultCellStyle.Format = "N4";
        }
    }
}

Best regards,

  • 1
    You are changing a property in the current style and not the style itself. That's why the event does not fire. Replace the code with `dgv.Columns[1].DefaultCellStyle = new DataGridViewCellStyle { Format = "N4" };` and debug to see what happens. –  Dec 21 '19 at 03:12
  • @JQSOFT: Not true. Quote form docs: "This event occurs when *any properties* of the `DataGridViewCellStyle` returned by the `DefaultCellStyle` property for a column is set to a new value". – P. Kouvarakis Dec 21 '19 at 06:57
  • @P.Kouvarakis You need to try what you read. –  Dec 21 '19 at 08:16
  • @P.Kouvarakis Actually, your comment has the answer. Read from _returned by the Def..._ –  Dec 21 '19 at 08:24
  • @JQSOFT: I tried your sugestion and i saw what huppand. EVERY other properties (like Alignment, BackColor, Font ...) are lost. That is not what i need. I waht to catch everytime when any ColumnDefaultCellStyle property is changed. If it is not possible :( i am not sure how to create own custom event. Please everyone for help. – DatProgramming Dec 22 '19 at 12:15
  • To preserve the required properties, and change one or more properties, and fire that event, you need to do `var style = dgv.Columns[1].DefaultCellStyle.Clone();` `style.Format = "N4";` `dgv.Columns[1].DefaultCellStyle = style`. Hope it is CLEAR now. –  Dec 22 '19 at 13:45
  • @JQSOFT: This is not what the docs say. The docs say you can take the object returned by `DefaultCellStyle`, change any of its properties and that would cause the event to fire. The fact that this does not happen means that either the docs are wrong (need to be corrected) or the implementation is wrong (bug). Either way there is somehing worng. – P. Kouvarakis Dec 23 '19 at 15:22
  • @P.Kouvarakis What docs? read the real thing for better understanding [DataGridViewCellStyle](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGridViewCellStyle.cs,2513e29e4134ab24) and only when you _digest_ that, you could judge and say _not true_, _something wrong_. with some proves and testing results. –  Dec 23 '19 at 18:42
  • Docs exist for a number of reasons: 1) so you don't have to read through thousands of lines of code to figure out how to use it, 2) To serve as a reference of what a piece of code SHOULD do (in lack of proper specification), 3) so that consumers of that piece of code do not depend on the actual implementation. So to cut a long story short, I can read the code but I shouldn't have to, no one should (except the one who has to fix it) – P. Kouvarakis Dec 23 '19 at 19:36
  • @P.Kouvarakis Good points. –  Dec 23 '19 at 20:08
  • @JQSOFT your solution is good and it work BUT my problem is different: i have custom class derived from DataGridView class There i make sum of numeric columns and display them in another DataGridView and i want to have the same format. That class is used by other developers and i cant tell them which way to use to change format (some of them i dont know) So what if they use dgv.Columns[1].DefaultCellStyle.Format = "N4"; ...that is normal – DatProgramming Dec 26 '19 at 21:01
  • So probably i need to think to create my own event. Probably to keep old format and in OnPaint() void compare active format with old format, so if they are not the same to use your solution and fire ColumnDefaultCellStyleChanged event I will appreciate some other solutions Thanks, (I think too that it is a bug or missed situation) – DatProgramming Dec 26 '19 at 21:02
  • 2 against 1 then you win guys. If you have time to edit your post to clear up your requirements, it will help to get better answers. Check your comments and you'll find different problems and requirements besides what already mentioned in your post. –  Dec 26 '19 at 22:42

0 Answers0