[c#] How to change column width in DataGridView?

I have created a database and table using Visual Studio's SQL Server Compact 3.5 with a dataset as my datasource. On my WinForm I have a DataGridView with 3 columns. However, I have been unable to figure out how to get the columns to take up the full width of the DataGridView which is shown in the image below.

I want to make the abbreviates column wider and then have the description column extend all the way over to the edge of the form. Any suggestions?

Update:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuickNote
{
public partial class hyperTextForm : Form
{
    private static hyperTextForm instance;

    public hyperTextForm()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Normal;
        this.MdiParent = Application.OpenForms.OfType<Form1>().First();            
    }

    public static hyperTextForm GetInstance()
    {
        if (instance == null || instance.IsDisposed)
        {
            instance = new hyperTextForm();
        }

        return instance;
    }

    private void abbreviationsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
    {
        this.Validate();
        this.abbreviationsBindingSource.EndEdit();
        this.tableAdapterManager.UpdateAll(this.keywordDBDataSet);
    }

    private void hyperTextForm_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'keywordDBDataSet.abbreviations' table. You can move, or remove it, as needed.
        this.abbreviationsTableAdapter.Fill(this.keywordDBDataSet.abbreviations);
        abbreviationsDataGridView.Columns[1].Width = 60;
        abbreviationsDataGridView.Columns[2].Width = abbreviationsDataGridView.Width - abbreviationsDataGridView.Columns[0].Width - abbreviationsDataGridView.Columns[1].Width - 72;
    }
}
}

This question is related to c# sql-server winforms sql-server-ce compact-database

The answer is


You could set the width of the abbrev column to a fixed pixel width, then set the width of the description column to the width of the DataGridView, minus the sum of the widths of the other columns and some extra margin (if you want to prevent a horizontal scrollbar from appearing on the DataGridView):

dataGridView1.Columns[1].Width = 108;  // or whatever width works well for abbrev
dataGridView1.Columns[2].Width = 
    dataGridView1.Width 
    - dataGridView1.Columns[0].Width 
    - dataGridView1.Columns[1].Width 
    - 72;  // this is an extra "margin" number of pixels

If you wanted the description column to always take up the "remainder" of the width of the DataGridView, you could put something like the above code in a Resize event handler of the DataGridView.


Set the "AutoSizeColumnsMode" property to "Fill".. By default it is set to 'NONE'. Now columns will be filled across the DatagridView. Then you can set the width of other columns accordingly.

DataGridView1.Columns[0].Width=100;// The id column 
DataGridView1.Columns[1].Width=200;// The abbrevation columln
//Third Colulmns 'description' will automatically be resized to fill the remaining 
//space

In my Visual Studio 2019 it worked only after I set the AutoSizeColumnsMode property to None.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to sql-server

Passing multiple values for same variable in stored procedure SQL permissions for roles Count the Number of Tables in a SQL Server Database Visual Studio 2017 does not have Business Intelligence Integration Services/Projects ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database How to create temp table using Create statement in SQL Server? SQL Query Where Date = Today Minus 7 Days How do I pass a list as a parameter in a stored procedure? SQL Server date format yyyymmdd

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to sql-server-ce

How to change column width in DataGridView? Creating stored procedure with declare and set variables How to connect to SQL Server from another computer? how to open *.sdf files? How do you open an SDF file (SQL Server Compact Edition)? Connection string with relative path to the database file How do you create a foreign key relationship in a SQL Server CE (Compact Edition) Database?

Examples related to compact-database

How to change column width in DataGridView?