【问题标题】:Winform Datagridview button column button with multiline text带有多行文本的 Winform Datagridview 按钮列按钮
【发布时间】:2021-12-16 12:30:41
【问题描述】:

我根据以下帖子成功创建了一个按钮列,包括向各个按钮添加文本:

How to add a button to a column in the DataGridView

问题是,我的文本不能放在一行中,所以我希望创建一个多行按钮标签。

这是我用来定义列的代码,但 Environment.Newline 似乎不适用于这些按钮。有没有办法为 datagridview 按钮创建多行按钮标签,如果有,如何?

        Dim inspectionNotesButtonCol As New DataGridViewButtonColumn With {
            .Name = "InspNotesButton",
            .HeaderText = "Insp." + Environment.NewLine + "Notes",
            .Text = "Insp." + Environment.NewLine + "Notes",
            .UseColumnTextForButtonValue = True,
            .Width = 50
        }
        Me.dgvIssues.Columns.Add(inspectionNotesButtonCol)

感谢 C# 或 vb.net 中的答案。

【问题讨论】:

    标签: .net winforms datagridview datagridviewbuttoncolumn


    【解决方案1】:

    您需要将MultiLine 属性设置为True,然后尝试使用Environment.NewLine。有几个控件共享您所经历的行为,TextBox 是另一个示例。

    【讨论】:

    • DatagridViewButtonCell 或 DataGridViewButtonColumn 没有多行选项。我会将 Multiline 设置为什么对象?
    • @EdwardBagby 你是对的,我错了。因此,让我们尝试使用 \n 而不是 Environment.NewLine。如果可行,我将编辑我的答案。如果没有,我会删除它。
    【解决方案2】:

    设置网格DefaultCellStyle.WrapMode 和AutoSizeRowsMode 应该允许您在按钮单元格中显示多行。像……

    Dim inspectionNotesButtonCol As New DataGridViewButtonColumn With {
            .Name = "InspNotesButton",
            .HeaderText = "Insp." + Environment.NewLine + "Notes",
            .Text = "Insp." + Environment.NewLine + "Notes",
            .UseColumnTextForButtonValue = True,
            .Width = 50
        }
    dgvIssues.Columns.Add(inspectionNotesButtonCol)
    dgvIssues.DefaultCellStyle.WrapMode = DataGridViewTriState.True
    dgvIssues.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
    dgvIssues.Rows.Add()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 2022-12-30
      • 1970-01-01
      相关资源
      最近更新 更多