c# - 设置winform DataGridView的显示格式

标签 c# winforms datagrid

我是 WinForms 开发的新手,目前我正在维护一个用 .Net 2.0 开发的应用程序

在应用程序中,我有一个名为“长度”的网格,它显示带有单位的值。我已使用 CellFormatting 事件来格式化单元格值,否则它只是数字。

但是当用户开始编辑时,我不希望显示单位,应该允许用户仅输入数字。

有没有直接的方法可以做到这一点?要在网格上设置的事件或属性?

enter image description here

最佳答案

您应该在 DataGridView_CellFormatting 事件中设置单位

void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        int value;
        if(e.Value != null && int.TryParse(e.Value.ToString(), out value))
        {
            e.Value = value.ToString("#mm");
        }
    }
}

关于c# - 设置winform DataGridView的显示格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11340292/

相关文章:

c# - 使用 AspNetCore.OData 7.2.1 GetRouteData 始终为 null

c# - 如何检测 ToolStripTextBox 中的 Alt + 左键

c# - 如何去除 ToolStrip Winforms 控件一角的这种奇怪的视觉伪像?

windows - 允许在资源管理器样式的 ListView 中选择从第一列开始

WPF - 在具有自定义复选框样式的 DataGrid 中无法使用 IsReadOnly

c# - 如何在 C# 中不使用 bigint 来添加或减去非常大的数字?

c# - 将 c# windows 面板转换为 c HWND

c# - WPF DataGrid 自动生成列顺序

c# - WPF DataGrid ItemsSource 问题

c# - 在WPF中绑定(bind)高度和宽度时如何减少绑定(bind)异常的数量?