设置显示格式,我是新来的WinForms开发,目前我保持.NET 2.0中的WinForm的DataGridView的
开发应用程序中的应用程序,我有这显示与单位值列称为长格。我已经使用CellFormatting
事件来格式化单元格值,否则它只是数字。
但是,当用户开始编辑我不想要单位显示,用户应该被允许输入唯一的数字。
有什么简单的方法可以做到吗?要在网格上设置的事件或属性?
设置显示格式,我是新来的WinForms开发,目前我保持.NET 2.0中的WinForm的DataGridView的
开发应用程序中的应用程序,我有这显示与单位值列称为长格。我已经使用CellFormatting
事件来格式化单元格值,否则它只是数字。
但是,当用户开始编辑我不想要单位显示,用户应该被允许输入唯一的数字。
有什么简单的方法可以做到吗?要在网格上设置的事件或属性?
您应该设置单元事件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");
}
}
}
,您可以设置格式字符串中使用CellStyle Builder中设置的自定义格式#毫米
怎么做:
您应该处理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)事件 – JohnnBlade