开发者

C# Datagridview resize programmatically

I have a datagridview that is populated and added to the winform programmatically.

The number of columns within the 开发者_Python百科gridview can vary greatly, so I want to change the size of the actual Gridview width to fit the number of columns.

Could anybody suggest how to do this?

Thanks.


Maybe I didn't understand it, but can't you just modify the width property of the grid each time you add a column?

DatagridView1.Width += 100; //estimated width of the column

EDIT: Ok, so you add the columns binding it to a datasource. But later, you con do something like:

foreach (DataGridViewColumn col in DataGridView1.Columns)
{
                    //do things
}

Other solution could be redraw you form, and put the grid inside a panel, and set the dock property of the grid to Fill. The grid will use all the space available, and if more is needed, then will show scroll bars.


In addition to what Jonathan said, you can set the FillWeight property for each column if you want it to be resized automatically:

buttonTest.Width = 80; //this is not resizable because we might have rendering problems
buttonTest.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

DescriptionColumn.FillWeight = 40; //for a text column, we can set it to be resized 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜