How to custom format data in datagridview during databinding

前端 未结 7 789
忘掉有多难
忘掉有多难 2020-12-16 14:21

I\'m looking for a way to format DataGridViewTextBoxColumn so that the value to be databinded is formatted during databinding. For example I have a CompanyName property and

7条回答
  •  误落风尘
    2020-12-16 15:13

    It sounds like IFormatProvider is exactly what you need. Then you can define different codes for the different formats you want for the different views.

    From Codeproject.

    public override string ToString()
    {
        return ToString("g", null); // Always support "g" as default format.
    }
    
    public string ToString(string format)
    {
      return ToString(format, null);
    }
    
    public string ToString(IFormatProvider formatProvider)
    {
      return ToString(null, formatProvider);
    }
    
    public string ToString(string format, IFormatProvider formatProvider)
    {
      if (format == null) format = "g"; // Set default format, which is always "g".
      // Continue formatting by checking format specifiers and options.
    }
    

提交回复
热议问题