设置 DataGridViewColumn 中单元格的格式

发布于 2024-11-04 22:28:52 字数 217 浏览 1 评论 0原文

如何在 datagridview 单元格中向用户显示值为:12345678 的字符串:1234/56/78
我认为我应该使用 DataGridViewColumnDefaultCellStyle.Format 属性,但我不知道合适的值是什么。
我正在使用 .NET Framework 2.0

How can I display the string with value: 12345678 to the user in a datagridview cell as: 1234/56/78.
I think I should use the DefaultCellStyle.Format property of the DataGridViewColumn, But I don't know what the appropriate value is.
I'm using .NET Framework 2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-11-11 22:28:52

您可以根据您的情况设置这样的格式

foreach (DataGridViewRow var in dataGridView1.Rows)
{
    (var.Cells[7] as DataGridViewTextBoxCell).Style.Format = "0000/00/00";
}

you can set the format like this for your case

foreach (DataGridViewRow var in dataGridView1.Rows)
{
    (var.Cells[7] as DataGridViewTextBoxCell).Style.Format = "0000/00/00";
}
留蓝 2024-11-11 22:28:52
using System;
using System.Windows.Forms;

namespace DGVFormatColumn
{
   public class MyCell : DataGridViewTextBoxCell
   {
      protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
      {
         String fullString = Value as String;

         // if we don't have an 8 character string, just call the base method with the values which were passed in
         if (fullString == null || fullString.Length != 8 || IsInEditMode)
            return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);

         // split the string into 3 parts
         String[] parts = new String[3];
         parts[0] = fullString.Substring(0, 4);
         parts[1] = fullString.Substring(4, 2);
         parts[2] = fullString.Substring(6, 2);

         // combine the parts with a "/" as separator
         String formattedValue = String.Join("/", parts);

         return formattedValue;
      }
   }

   class MyForm : Form
   {
      public MyForm()
      {
         // create a datagridview with 1 column and add it to the form
         DataGridView dgv = new DataGridView();
         DataGridViewColumn col = new DataGridViewColumn(new MyCell()); // use my custom cell as default cell template
         dgv.Columns.Add(col);
         this.Controls.Add(dgv);
      }
   }

   static class Program
   {
      static void Main()
      {
         Application.Run(new MyForm());
      }
   }
}
using System;
using System.Windows.Forms;

namespace DGVFormatColumn
{
   public class MyCell : DataGridViewTextBoxCell
   {
      protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
      {
         String fullString = Value as String;

         // if we don't have an 8 character string, just call the base method with the values which were passed in
         if (fullString == null || fullString.Length != 8 || IsInEditMode)
            return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);

         // split the string into 3 parts
         String[] parts = new String[3];
         parts[0] = fullString.Substring(0, 4);
         parts[1] = fullString.Substring(4, 2);
         parts[2] = fullString.Substring(6, 2);

         // combine the parts with a "/" as separator
         String formattedValue = String.Join("/", parts);

         return formattedValue;
      }
   }

   class MyForm : Form
   {
      public MyForm()
      {
         // create a datagridview with 1 column and add it to the form
         DataGridView dgv = new DataGridView();
         DataGridViewColumn col = new DataGridViewColumn(new MyCell()); // use my custom cell as default cell template
         dgv.Columns.Add(col);
         this.Controls.Add(dgv);
      }
   }

   static class Program
   {
      static void Main()
      {
         Application.Run(new MyForm());
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文