DataGridView可以在同一列中使用不同的单元格格式? [英] DataGridView Different cell formatting in same column possible?

查看:608
本文介绍了DataGridView可以在同一列中使用不同的单元格格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带DataGridView的WinForms应用程序来显示一些数据。




对我来说重要的是especcialy两列。一个是单位,第二个是价值。单位格式中有字符串,值具有数值。我想根据单位是什么格式化DataGridViewRow中的值。例如:如果单位是piece,我需要Value为int(不带小数位)。如果单位为'mm',我需要将值分成1000并显示二(2)位小数,并将单位更改为'm'...希望你明白。还有一些图形解释:

 单位值---->想要显示格式---->单位价值
piece 100,00 piece 100
mm 10000,00 m 10,00
g 100000,00 kg 100,00

什么是正确的方式来亵渎这个?
感谢您的帮助。

解决方案

您将需要处理 CellFormatting() 事件。在这里,您可以访问当前行和列。



在这种情况下,您可以访问 DataGridViewCellFormattingEventArgs 类型:

 公共属性CellStyle 
公共属性ColumnIndex
公共属性DesiredType
公共属性FormattingApplied
公共属性RowIndex
公共属性值

因此,可以使用 RowIndex 属性来查找数据源中的值,以获取条件格式的其他列值。



我目前还没有VS方便,但如下所示:

  private void OnCellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if(e.ColumnIndex == ValueColIndex)
{
string unit = datasource.Rows [e.RowIndex ] [ValueColIndex] .ToString();
开关(单位)
{
casepiece:e.Value = Convert.ToInt32(e.Value).ToString();
e.FormattingApplied = true;
break;
casemm:e.Value = string.Format({0:00},Convert.ToDouble(e.Value)/ 1000);
e.FormattingApplied = true;
break;
// etc
}
}
}

PS,你可能需要处理任何例外。


Again need help from this awsome comunity.

I have a WinForms app with DataGridView to display some data. There are especcialy two columns that are important to me. One is "Unit" and the second one is "Value". Unit has strings inside format and Value has numeric values. I would like to format the Value in each DataGridViewRow depending on what the Unit is. For example: if unit is 'piece', I need Value to be int (no decimal places). If unit is 'mm', I need to divide Value with 1000 and show two (2) decimal places and also change Unit to 'm'... Hope you understand. Also some graphic interpretation:

Unit   Value    ----> Wanted display format ----> Unit   Value
piece  100,00                                     piece   100
mm     10000,00                                   m       10,00
g      100000,00                                  kg      100,00

What would be the right way to aproach this? Thank you for your help.

解决方案

You will need to handle the CellFormatting() event. In here, you can get access to the current row and column.

In this event you have access to the following properties in the DataGridViewCellFormattingEventArgs type:

Public property CellStyle
Public property ColumnIndex
Public property DesiredType
Public property FormattingApplied   
Public property RowIndex    
Public property Value   

Thus you can use the RowIndex property to look up the value in your datasource to get the other columns value for your conditional formatting.

I haven't got VS handy at the moment but something like:

private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  if (e.ColumnIndex == ValueColIndex)
  {
    string unit = datasource.Rows[e.RowIndex][ValueColIndex].ToString();
    switch(unit)
    {
      case "piece": e.Value = Convert.ToInt32(e.Value).ToString();
       e.FormattingApplied = true;
       break;
      case "mm": e.Value = string.Format("{0:00}", Convert.ToDouble(e.Value) / 1000);
       e.FormattingApplied = true;
       break;
      //etc
    }
  }
}

PS, you may need to handle any exceptions in there.

这篇关于DataGridView可以在同一列中使用不同的单元格格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆