none
Why is DataGridViewCellStyle.Format being ignored? RRS feed

  • Question

  • I have a DataGridView with three columns. Its DataSource is a DataTable with three Int32 columns. I explicitly set the middle column of the view to use format string "000" as I need it to always show three digits using leading zeroes as a filler when needed. But it never shows the leading zero.

    Below is the code. I have included the various other bits that are going on just in case any of this would impact the formatting (although I don't see how it could, but then by my understanding what I have should work and it doesn't, so clearly I don't know what I'm doing).

            // Data definitions at the start of the class

            private DataTable windTable_m = null;

            // Later on - initialisation code

            windTable_m = new DataTable();
            DataColumn windAltCol = new DataColumn("Alt (ft)");
            windTable_m.Columns.Add(windAltCol);
            DataColumn windDirCol = new DataColumn("Dir (T)");
            windTable_m.Columns.Add(windDirCol);
            DataColumn windSpeedCol = new DataColumn("Speed (kt)");
            windTable_m.Columns.Add(windSpeedCol);
            windGrid.DataSource = windTable_m;

            windGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            windGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            DataGridViewCellStyle hdrStyle = new DataGridViewCellStyle();
            Padding padding = new Padding(0, 1, 0, 1);
            hdrStyle.Padding = padding;
            windGrid.ColumnHeadersDefaultCellStyle = hdrStyle;
            Padding margin = new Padding(0);
            windGrid.Margin = margin;
            windGrid.Columns[0].FillWeight = 1;
            windGrid.Columns[1].FillWeight = 1;
            windGrid.Columns[1].DefaultCellStyle.Format = "000";
            windGrid.Columns[2].FillWeight = 1;

            // Even later - actual data from an input form being transferred via Dictionary "windAlts_m"

            // to windTable_m, which is the DataSource for the grid view.

            windTable_m.Rows.Clear();
            foreach (Int32 alt in windAlts_m.Keys)
            {
                windTable_m.Rows.Add(alt, windAlts_m[alt].DirFrom, windAlts_m[alt].Speed);
            }


    Incidentally, I have checked the format string "000" using Microsoft's downloadable format checker utility and it works fine there.
    Tuesday, July 9, 2019 3:18 PM

Answers

  • Hello,

    For the DataColumn that in the DataGridView needs formatting, try setting the data type for the DataColumn.

    windTable_m.Columns.Add(new DataColumn() {ColumnName = "TODO", DataType = typeof(Int32)});
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 9, 2019 3:42 PM
    Moderator
  • Try this modification:

       DataColumn windDirCol = new DataColumn( "Dir (T)", typeof(int) );

    Tuesday, July 9, 2019 3:44 PM

All replies

  • Hello,

    For the DataColumn that in the DataGridView needs formatting, try setting the data type for the DataColumn.

    windTable_m.Columns.Add(new DataColumn() {ColumnName = "TODO", DataType = typeof(Int32)});
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 9, 2019 3:42 PM
    Moderator
  • Try this modification:

       DataColumn windDirCol = new DataColumn( "Dir (T)", typeof(int) );

    Tuesday, July 9, 2019 3:44 PM
  • Thanks Karen. DataGridView does not have DataColumn columns - they are used by DataTable. DataGridView uses DataGridViewColumn. It has an equivalent though so it's definitely one worth trying, and the equivalent is:

        windGrid.Columns[1].ValueType = Type.GetType("System.Int32");

    I just tried inserting this ahead of the line that sets the format string. Unfortunately it made no difference.

    Tuesday, July 9, 2019 9:33 PM
  • Given that Viorel_ also made the same suggestion re the DataColumn, I just tried your suggestion in the form you made it (made no sense to me because that's the data source rather than the grid view itself), and it worked fine, so thanks to both of you.

    I still don't understand why I'm setting the type on the source rather than the grid, especially when the data went into the source as an integer anyway (i.e. the source should know what its data type is).

    Tuesday, July 9, 2019 9:39 PM
  • Greetings,

    Here is how I see it, there are two camps, one camp prefers strong typing from the original data source then the DataGridView picks up on this, this is my camp while the other camp tends to lean on the DataGridView for types which is fine but usually gets quirky in some cases especially formatting of cells.

    On the first camp, setting the DataPropertyName does the same as not creating columns and allowing the DataGridView to figure the types out which works for DataSet/DataTable, DataTable, List(Of T) and Entity Framework (sometimes to well for EF).  


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 9, 2019 10:19 PM
    Moderator
  • [...]

    I still don't understand why I'm setting the type on the source rather than the grid, especially when the data went into the source as an integer anyway (i.e. the source should know what its data type is).

    Using DataColumn, you actually define the destination column of Data Grid.

    If you do not specify the type, then string type is used (according to documentation for DataColumn) and all of the values are converted to string. But “000” is not a valid format for strings.

    When you specify typeof(int), then the values are stored as numbers. If strings are supplied, they are converted to int.


    Wednesday, July 10, 2019 4:43 AM