Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Attribute I can use in my class to tell DataGridView not to create a column for it when bound to a List<MyClass>

I have a class like this:

private class MyClass {   [DisplayName("Foo/Bar")]   public string FooBar { get; private set; }   public string Baz { get; private set; }         public bool Enabled; } 

When I create a List<MyClass> and assign it to the DataSource of a DataGridView, the grid shows me two columns, "Foo/Bar" and "Baz". This is what I want to happen.

It currently works because Enabled is a field, not a property - DataGridView will only pick up properties. However, this is a dirty hack.

I would like to make Enabled a property too, but still hide it on the DataGridView.

I know I can manually delete the column after binding.. but that's not ideal.

Is there an attribute similar to DisplayName, that I can tag a property with? Something like [Visible(false)] ?

like image 533
Blorgbeard Avatar asked Jul 01 '09 01:07

Blorgbeard


People also ask

How do I make DataGrid read-only?

To make a column read-only programmaticallySet the DataGridViewColumn. ReadOnly property to true .

What is the difference between DataGrid and DataGridView?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

How set DataGridView size in C#?

To create fill-mode columns for values of varying size and importance. Set the DataGridView. AutoSizeColumnsMode property to Fill to set the sizing mode for all columns that do not override this value. Set the FillWeight properties of the columns to values that are proportional to their average content widths.

Which property of a DataGridView is used to visualize data in a Windows Form?

The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor. For more information, see Basic Formatting and Styling in the Windows Forms DataGridView Control.


2 Answers

[Browsable(false)] will hide a property from a DataGridView.

A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true. These members can be modified at design time. Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer. The default is true.

like image 56
C-Pound Guru Avatar answered Sep 22 '22 15:09

C-Pound Guru


I've been blissfully unaware that the System.ComponentModel decorator attributes like BrowsableAttribute and it's kin were related to anything other than binding to a PropertyGrid. (facepalm) I like C-Pound Guru's approach because it allows you to keep your GUI more loosely coupled than what I've done in the past.

Just for a different perspective, the approach I've used for a long time is to pre-define columns in your DataGridView, either programatically or through the Form Designer. When you do this, you can set each column's DataPropertyName to the name of your property. The only trick is that you need to set the DataGridView's AutoGenerateColumns property to false otherwise the DGV will completely ignore your manually created columns. Note that, for whatever reason, the AutoGenerateColumns property is hidden from the Form Designer's Property Grid...no idea why. The one advantage I see to this approach is that you can pre-set the column formatting and such - you don't have to bind and then go tweak column rendering/sizing settings.

Here's an example of what I mean:

_DGV.AutoGenerateColumns = false; DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn(); textColumn.DataPropertyName = "FooBar"; textColumn.HeaderText = "Foo/Bar"; // may not need to do this with your DisplayNameAttribute _DGV.Columns.Add(textColumn); textColumn = new DataGridViewTextBoxColumn(); textColumn.DataPropertyName = "Baz";  List<MyClass> data = GetMyData(); _DGV.DataSource = data; 
like image 27
Yoopergeek Avatar answered Sep 22 '22 15:09

Yoopergeek