DataGridView - how to set column width?

后端 未结 13 2304
情书的邮戳
情书的邮戳 2020-12-13 23:58

I have a WinForms application with DataGridView control. My control has five columns (say \"Name\", \"Address\", \"Phone\" etc)

I am not happy with defa

13条回答
  •  既然无缘
    2020-12-14 00:54

    Most of the above solutions assume that the parent DateGridView has .AutoSizeMode not equal to Fill. If you set the .AutoSizeMode for the grid to be Fill, you need to set the AutoSizeMode for each column to be None if you want to fix a particular column width (and let the other columns Fill). I found a weird MS exception regarding a null object if you change a Column Width and the .AutoSizeMode is not None first.

    This works

    chart.AutoSizeMode  = DataGridViewAutoSizeColumnMode.Fill;
    ... add some columns here
    chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    chart.Column[i].Width = 60;
    

    This throws a null exception regarding some internal object regarding setting border thickness.

    chart.AutoSizeMode  = DataGridViewAutoSizeColumnMode.Fill;
    ... add some columns here
     // chart.Column[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    chart.Column[i].Width = 60;
    

提交回复
热议问题