0

我遇到了一个问题,每当我的行高增加时,按钮的高度也会增加,从而创建:

在此处输入图像描述

如何将此按钮的宽度设置为 20?另外,图像列也可以包含文本吗?

4

2 回答 2

3

您可以通过在行更改高度时更改单元格的填充来模拟这一点。

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        ClientSize = new Size(640, 480);
        DataGridView dgv = new DataGridView
            {
                Dock = DockStyle.Fill,
                Font = new Font(FontFamily.GenericSansSerif, 36),
                AutoGenerateColumns = false,
                AllowUserToAddRows = false,
                RowHeadersVisible = false,
                ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
                Columns =
                {
                    new DataGridViewTextBoxColumn { HeaderText = "TextBox", DataPropertyName = "Text", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, },
                    new DataGridViewButtonColumn { Name = "ButtonColumn", HeaderText = "Button", AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader, DefaultCellStyle = new DataGridViewCellStyle { Padding = new Padding(2) } },
                },
                DataSource = Enumerable.Range(1, 5).Select(i => new { Text = "Item " + i.ToString() }).ToList(),
            };

        dgv.RowHeightChanged += (s, e) =>
            {
                DataGridViewCell cell = e.Row.Cells["ButtonColumn"];
                int pad = (e.Row.Height - 25) / 2;
                cell.Style.Padding = new Padding(2, pad, 2, pad);
            };

        Controls.Add(dgv);
    }
}
于 2012-07-19T19:27:36.450 回答
0

请确保按钮 button1.AutoSize = true;设置为 false

http://msdn.microsoft.com/en-us/library/system.windows.forms.button.autosizemode

于 2012-07-19T16:39:33.540 回答