0

Some of the columns in my DataGridView hold times. Others hold dates. Currently, the only way to know which is which is to inspect their naming convention. I would much rather this knowledge be held somewhere in the grid itself. Is there any feature that provides this?

Frame challenges are welcome. What I really want is to be able to tell a column that is intended for one job from a column that is intended for another.

My current, crude solution is to inherit from the type that I wanted to use and add a property that marks down what I want e.g.

using System.Windows.Forms;
namespace foo;

public class TimeCol : DataGridViewTextBoxColumn
{
    public bool IsTime {get => true ;}
}

J. Mini
  • 1,868
  • 1
  • 9
  • 38
  • 1
    I assume that you're saying, without actually saying, that both columns contain `DateTime` values. Every column has a `Tag` property, so you can always put something in there that differentiates them. You can also create a custome column type and add a property of your own if the `Tag` property being of type `Object` is a problem. – jmcilhinney Jun 27 '23 at 11:12
  • @jmcilhinney Is the `Tag` property only available at run time? I can't see it in Design view. – J. Mini Jun 27 '23 at 11:13
  • 1
    That is correct. If you check the documentation for the property, you'll see it has the `Browsable` attribute set to `false`. I guess that's a reason to go with your own custom column types. In fact, if you were to create two custom types then you wouldn't even need a property because the type itself would tell you. I see that you already are using a custom type, but I assume that that was added with an edit. It's up to you but I would go with `DataGridViewTimeTextBoxColumn` and not break the naming convention. – jmcilhinney Jun 27 '23 at 11:27
  • If you're targeting .NET 6+, you could set the Type of the Columns to `TimeOnly` and `DateOnly`, so maybe you don't need to check the Type either -- In case you can do that, I know that the constructors of these structs are kind of lacking *logical* overloads... – Jimi Jun 27 '23 at 11:30
  • You may want to take a look at [DataAnnotations attributes for DataGridView in Windows Forms](https://stackoverflow.com/a/59885956/3110834). You can also extend it like what I did in [the other example](https://stackoverflow.com/a/66563841/3110834) to support cell editors as well. – Reza Aghaei Jun 27 '23 at 11:35

0 Answers0