Search code examples
c#datatabledatagridalignment

c# how to correctly align number in DataGrid


I have a working Windows Forms application that loads in transaction-data and categorizes it, adding the amount of euros spent on that certain category.

The amount of euros is shown in the dataTable as follows:

cells

but I want the correct alignment when it comes to representing numbers. So this is what I mean:

enter image description here

Now I know a devious way to dealing with this problem (since: how should a dataTable-object itself know how to align to my wishes?)

possible solution

But I would like to know if there is a better approach for this situation.


Solution

  • I'm assuming it's Windows Forms application.

    Below is a sample app with a DataGrid with 2 columns, one of which has its values right-aligned. The essential line here is

    Alignment = HorizontalAlignment.Right
    

    The app (copy/paste and run it to see the result):

    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Form1 : System.Windows.Forms.Form
    {
        public static void Main()
        {
            Application.Run(new Form1());
        }
    
        public Form1()
        {
            var myDataGrid = new DataGrid();
            ClientSize = new System.Drawing.Size(450, 330);
            myDataGrid.Location = new Point(24, 50);
            myDataGrid.Size = new Size(300, 200);
    
            Controls.Add(myDataGrid);
    
            myDataGrid.SetDataBinding(MakeDataSet(), "MyTable");
    
            var tableStyle = new DataGridTableStyle { MappingName = "MyTable" };
            var nameColumnStyle = new DataGridTextBoxColumn { MappingName = "Name" };
            var sumColumnStyle = new DataGridTextBoxColumn
            {
                MappingName = "Sum",
                Width = 170,
                Alignment = HorizontalAlignment.Right
            };
            tableStyle.GridColumnStyles.Add(nameColumnStyle);
            tableStyle.GridColumnStyles.Add(sumColumnStyle);
            myDataGrid.TableStyles.Add(tableStyle);
        }
    
        private DataSet MakeDataSet()
        {
            var dataSet = new DataSet("myDataSet");
            var table = new DataTable("MyTable");
            var nameCol = new DataColumn("Name");
            var sumCol = new DataColumn("Sum", typeof(float));
            table.Columns.Add(nameCol);
            table.Columns.Add(sumCol);
    
            dataSet.Tables.Add(table);
    
            var row1 = table.NewRow();
            row1["Name"] = "Bank";
            row1["Sum"] = 1.25;
            table.Rows.Add(row1);
    
            var row2 = table.NewRow();
            row2["Name"] = "Hosting";
            row2["Sum"] = 12.5;
            table.Rows.Add(row2);
    
            return dataSet;
        }
    }
    

    UPDATE

    Here's an example of the custom alignment you need, it's a console app but the same code will work in a Windows Forms app:

        class Program
        {
            static void Main(string[] args)
            {
                var numbers = new[] { 1, 100.12, 50.218, 0.5 };
                int length = 10;
                var separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
    
                foreach (var n in numbers.Select(i => i.ToString()))
                {
                    var offset = n.Contains(separator) ? n.Length - n.IndexOf(separator) : 0;
                    string format = string.Format("{{0, {0}}}", length + offset);
                    Console.WriteLine(string.Format(format, n));
                }
            }
        }
    

    Tweak the variable length to your needs.