“垫子”列标题上的宽度? [英] "Cushion" width on a column header?

查看:53
本文介绍了“垫子”列标题上的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小循环功能,可根据列标题文本的宽度来调整列的大小:

I have a little loop function that resizes the columns based on the width of the text of the Column Header:

var columns = VView.gridViewCblID.Columns;

foreach (DataGridViewColumn clm in columns)
{
    VView.lblDummy.Text = clm.HeaderText;
    if (clm.Width > VView.lblDummy.Width && clm.Width <= 100)
    {
        clm.Width = VView.lblDummy.Width;
    }
}

但是,由于垫子是自动应用于列HeaderText的左侧,列将呈现束缚外观:

However, due to a "cushion" that is automatically applied to the left of the Column HeaderText, the columns get a "bunched" appearance:

该缓冲垫的实际宽度是多少,因此我可以将其应用于该方法?即

What is the actual width of that cushion, so I can apply it to the method? I.e.

clm.Width = VView.lblDummy.Width + (cushion *2);


推荐答案

可以使用 AutoSizeColumnsMode 为您完成工作,然后将调整大小模式重新设置为手动。例如

It's possible to use AutoSizeColumnsMode to do the work for you, and then set the resize mode back to manual. E.g.

DataGridView dgv = new DataGridView() { Dock = DockStyle.Fill };
dgv.Columns.Add("Cable Number", "Cable Number");
dgv.Columns.Add("Type", "Type");
dgv.Columns.Add("Length", "Length");

dgv.Columns["Type"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;

Form f = new Form();
f.Controls.Add(dgv);
dgv.HandleCreated += delegate {
    dgv.BeginInvoke((Action) delegate {
        var c = dgv.Columns["Type"];
        int w = c.Width;
        c.Width = w; // set current width, otherwise DGV reverts to previous 100 width
        c.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
    });
};

这篇关于“垫子”列标题上的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆