2012-07-05 73 views
2

设置显示格式,我是新来的WinForms开发,目前我保持.NET 2.0中的WinForm的DataGridView的

开发应用程序中的应用程序,我有这显示与单位值列称为长格。我已经使用CellFormatting事件来格式化单元格值,否则它只是数字。

但是,当用户开始编辑我不想要单位显示,用户应该被允许输入唯一的数字。

有什么简单的方法可以做到吗?要在网格上设置的事件或属性?

enter image description here

回答

1

您应该设置单元事件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"); 
     } 
    } 
} 
+0

我已经这样做了,这就是我如何显示1毫米。但是当细胞进入编辑模式时,我只想显示数字。 –

+0

或尝试使用DataGridView1_CellValueChanged(对象发件人,DataGridViewCellEventArgs e)事件 – JohnnBlade

1

,您可以设置格式字符串中使用CellStyle Builder中设置的自定义格式#毫米

怎么做:

  1. 右键点击网格,然后点击属性
  2. 在属性窗口中,单击会弹出了编辑列对话框
  3. 按钮选择要格式化
  4. 在编辑栏右侧的对话框中选择DefaultCellStyle属性的细胞
  5. 点击DefaultCellStyle属性,那么CellStyleBuilder对话框将打开
  6. 在这里,你的格式属性,这会给你的格式字符串对话框
  7. 设置自定义属性,以#MM,你会看到预览底部
  8. 单击确定...直到您回到您的网格...
1

您应该处理EditingControlShowing事件以更改当前单元格格式。

private void dataGridView1_EditingControlShowing(object sender, 
           DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (dataGridView1.CurrentCell.ColumnIndex == 1) 
     { 
      e.CellStyle.Format = "#"; 
      e.Control.Text = dataGridView1.CurrentCell.Value.ToString(); 
     } 
    } 
相关问题