C# 设置winform DataGridView的显示格式
我是WinForms开发的新手,目前正在维护一个在.NET2.0中开发的应用程序 在应用程序中,我有一个名为Length列的网格,它以单位显示值。我使用了C# 设置winform DataGridView的显示格式,c#,winforms,datagrid,C#,Winforms,Datagrid,我是WinForms开发的新手,目前正在维护一个在.NET2.0中开发的应用程序 在应用程序中,我有一个名为Length列的网格,它以单位显示值。我使用了CellFormatting事件来格式化单元格值,否则它就是数字 但当用户开始编辑时,我不希望显示该单元,应该只允许用户输入数字 有什么直截了当的方法吗?要在网格上设置的事件或属性 您应该在事件DataGridView\u中设置单位 void DataGridView_CellFormatting(object sender, DataGrid
CellFormatting
事件来格式化单元格值,否则它就是数字
但当用户开始编辑时,我不希望显示该单元,应该只允许用户输入数字
有什么直截了当的方法吗?要在网格上设置的事件或属性
您应该在事件DataGridView\u中设置单位
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");
}
}
}
可以使用CellStyle Builder设置字符串格式,也可以将自定义格式设置为#mm 如何做到:
您应该处理
EditingControlShowing
事件来更改当前单元格格式
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
e.CellStyle.Format = "#";
e.Control.Text = dataGridView1.CurrentCell.Value.ToString();
}
}
我已经在做了,这就是我如何显示1毫米。但是当单元格进入编辑模式时,我只想显示数字。或者尝试使用DataGridView1_CellValueChanged(对象发送者,DataGridViewCellEventArgs e)事件