为什么没有在C#中自动调整为DataGridView的行标题宽度工作? [英] Why doesn't AutoResize work for Row Header Width in DataGridView in C#?

查看:1018
本文介绍了为什么没有在C#中自动调整为DataGridView的行标题宽度工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataGridView控件新困惑是我这么多。我在与行表头宽度没有很好地拟合它里面的文本中的问题。我没有做搜索,但目前存在的解决方案并没有给我什么,我期待

I am new at DataGridView control and it confused me so much. I have a problem in with row header width doesn't have a good fit to the text inside of it. I did make a search but currently exist solutions didn't give me what I expect.

我使用这个代码来创建动态的DataGridView:

DataGridView CreateInputBox(int proc,int mac)
{
    DataGridView databox = new DataGridView();
    for (int i = 0; i < mac; i++)
    {
        databox.Columns.Add("col" + (i + 1), "M" + (i + 1));
        databox.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        databox.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
    }
    for (int i = 0; i < proc; i++)
    {
        databox.Rows.Add();
        databox.Rows[i].HeaderCell.Value = "P" + (i + 1);
    }
    databox.AllowUserToAddRows = false;
    databox.AllowUserToDeleteRows = false;
    databox.AllowUserToOrderColumns = false;
    databox.AutoSize = true;
    databox.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    databox.RowHeadersDefaultCellStyle.Padding = new Padding(3);

    return databox;
}



结果:

由于可以看出,P1,P2,P3行标题避风港 T A般配且有一定的空间。我希望他们能像M1,M2,M3列标题。我怎样才能解决这个问题呢?

As it can be seen, P1,P2,P3 row headers haven't a good fit and there is some space. I want them to be like M1,M2,M3 column headers. How can I solve this issue?

我要行标题的宽度适合行标题中没有显示记录选择的内容。

I want the width of row headers fit the content of row header without showing record selector.

推荐答案

DataGridView的计算应用文本的宽度,排图标的宽度和填充行标题的首选大小。

DataGridView calculates the preferred size of the row header by applying text width, row icon width and padding.

要更改其首选大小计算,同时防止绘制图标,你需要创建一个自定义行标题单元格继承的方式 DataGridViewRowHeaderCell 并重写的getPreferredSize 油漆方法:

To change the way which preferred size is calculated and also to prevent drawing icons, you need to create a custom row header cell inheriting DataGridViewRowHeaderCell and override GetPreferredSize and Paint methods:

public class CustomHeaderCell : DataGridViewRowHeaderCell
{
    protected override Size GetPreferredSize(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex, Size constraintSize)
    {
        var size1 = base.GetPreferredSize(graphics, cellStyle, rowIndex, constraintSize);
        var value = string.Format("{0}", this.DataGridView.Rows[rowIndex].HeaderCell.FormattedValue);
        var size2 = TextRenderer.MeasureText(value, cellStyle.Font);
        var padding = cellStyle.Padding;
        return new Size(size2.Width + padding.Left+padding.Right, size1.Height);
    }
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, DataGridViewPaintParts.Background);
        base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
        TextRenderer.DrawText(graphics, string.Format("{0}", formattedValue), cellStyle.Font, cellBounds, cellStyle.ForeColor);
    }
}



然后,它足以与新的来取代目前的头单元格头单元格:

Then it's enough to replace current header cells with your new header cell:

//Put this lines before adding rows
grid.RowTemplate.DefaultHeaderCellType = typeof(CustomHeaderCell);
grid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
grid.RowHeadersDefaultCellStyle.Padding = new Padding(2);

这篇关于为什么没有在C#中自动调整为DataGridView的行标题宽度工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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