Dynamically set column width for datagrid winforms?

I created the columns of my datagrid using this,

private void Receive_Load(object sender, System.EventArgs e) { DataGridView1.Columns.Add("Sender",typeof(string)); DataGridView1.Columns.Add("Time",typeof(string)); DataGridView1.Columns.Add("Message",typeof(string)); } 
  • How can I dynamically assign column width for datagrid winforms?
0
source share
2 answers

I think you are looking for something linear

 DataGridView1.Columns["ColumnName"].Width = 75; 

Hope this helps you.

In addition, you can set the AutoSizeMode column to automatically receive different forms of behavior. For example, if you set it to ColumnHeader, then the cell width will be set in the best way to display the title text. You can get more information in this link .

+1
source

Here's a suggestion: If you know the length of your fields, you can multiply their length by a constant value (for example, the maximum width of a character) to get a dynamic width.

-one
source

All Articles