Home > database >  How to put Price on the Right side of Column in DataGridView? (c#.net core) Tiny Problem
How to put Price on the Right side of Column in DataGridView? (c#.net core) Tiny Problem

Time:12-24

So apparently it looks better if you have the Price of a Product on the right side inside of a Column like this:

Ofcourse this is an OrderID but the look should be the same.

Here you can see the Number is on the Right side.

But at the moment mine looks more like this.

Picture of my current Column

Thanks for helping; C ya!

CodePudding user response:

Setup each column for the DataGridView in the designer by clicking the chevron

enter image description here

Select edit columns and select the column to set the alignment then select DefaultCellStyle and set alignment to MiddleRight.

Example using mocked data (works the same from a database table). All numeric columns have MiddleRight alignment below.

private void OnShown(object sender, EventArgs e)
{
    
    dataGridView1.AutoGenerateColumns = false;

    DataTable table = new DataTable();
    table.Columns.Add("Item", typeof(string));
    table.Columns.Add("Qty", typeof(int));
    table.Columns.Add("Price", typeof(decimal));
    table.Columns.Add("Total", typeof(decimal));

    table.Columns["Total"].Expression = "Qty * Price";

    table.Rows.Add("First item", 2, 60);
    table.Rows.Add("Second item", 2, 50);
    table.Rows.Add("Third item", 1, 20);

    dataGridView1.DataSource = table;

}

enter image description here

Manually setting up alignment.

var dataGridViewCellStyle1 = new DataGridViewCellStyle { Alignment = DataGridViewContentAlignment.MiddleRight };
PriceColumn.DefaultCellStyle = dataGridViewCellStyle1;
  • Related