Dynamically assign a column width to a winforms datagrid?

我只是一个虾纸丫 提交于 2019-12-11 18:17:30

问题


I ve created 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 a column width to a winforms datagrid?

回答1:


I think you are searching for something line

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

I Hope it help you.

In addition, you can set the AutoSizeMode of the column to obtain different behaviours automatically. For example, if you set it to ColumnHeader, then the cell width will be set to the best fit for showing the header text. You can get more info in this Link.




回答2:


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



来源:https://stackoverflow.com/questions/2175274/dynamically-assign-a-column-width-to-a-winforms-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!