Hi @Giorgio Sfiligoi , Welcome to Microsoft Q&A,
This is a common DPI scaling behavior, especially in Windows desktop applications. Although you can solve some of the problems by enabling DPI awareness in app.manifest
, you still need to manually adjust the column width and row height of the control to respond to the change in DPI scaling.
You can capture the DpiChanged event and adjust the layout of the control when the DPI changes. For Windows 10 and above, the DpiChanged event is available for the form.
protected override void OnDpiChanged(DpiChangedEventArgs e)
{
base.OnDpiChanged(e);
// Get the new DPI and adjust column width and row height
float scaleFactor = e.DeviceDpiNew / 96f;
foreach (ColumnHeader column in listView1.Columns)
{
column.Width = (int)(column.Width * scaleFactor);
}
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.Width = (int)(column.Width * scaleFactor);
}
dataGridView1.RowTemplate.Height = (int)(dataGridView1.RowTemplate.Height * scaleFactor);
}
You can also use the Windows API functions Graphics.DpiX and Graphics.DpiY to get the current system DPI Scale and dynamically adjust the column width and row height of the control.
private void Form1_Load(object sender, EventArgs e)
{
// Get the current system DPI scaling ratio
using (Graphics g = this.CreateGraphics())
{
float dpiX = g.DpiX;
float dpiY = g.DpiY;
// Assuming the default DPI is 96 (100% scaling), calculate the scaling ratio
float scaleFactor = dpiX / 96f;
// Adjust ListView column width
foreach (ColumnHeader column in listView1.Columns)
{
column.Width = (int)(column.Width * scaleFactor);
}
// Adjust DataGridView column width and row height
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
column.Width = (int)(column.Width * scaleFactor);
}
dataGridView1.RowTemplate.Height = (int)(dataGridView1.RowTemplate.Height * scaleFactor); } }
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.