views:

549

answers:

2

I'm binding a linq Query to a datagridview for the purposes of allowing a user to insert/update/delete. However I'd like to hide the Id column. but I'd rather not hard code the

datagridview.columns[id].visible=false

In the event the id column name changes. I've read that you can walk through reflection to get a run-time list of columns, but I can't have that checked at design-time for a column name. Is it possible?

If I change the query to not include the ID, then I'm hard coding the column list instead of the id column name, so if columns were added, I'd have to go manually update there instead.

A: 

Couldn't you do it in the html? Or are you using ASP.NET?

<asp:BoundField DataField="id" Visible="false" />
J.13.L
I didn't think dataGridView as in Asp.Net I thought it was called dataGrid there? Either way I'm in winforms
Maslow
Right. It's called DataGrid or GridView in ASP.NET. DataGridView is a WinForms control.
Joe Chung
+1  A: 

If you're using Winforms, then you should have a BindingSource on your Form that is typed to your LINQ-to-SQL type. If you do this, then you'll get full designer support for all of the fields available on that class.

If you're doing a lot of data binding work with LINQ-to-SQL entities (or any other classes, for that matter), I highly recommend checking into HyperDescriptor by Mark Gravell. It speeds up the UI data binding performance considerably by bypassing repeated runtime invocations of the properties on the classes.

Adam Robinson
The binding source only works here if I'm using design time databinding =/There's currently no such thing as partial properties so it's not like I could add an the [Browsable(false)] attribute to those columns.
Maslow
@Maslow: Your question seemed to indicate that you wanted to use something at design time. Is there a particular reason you don't want to/aren't able to use design time binding? In any event, there isn't a means of making an indexer (like datagridview.columns) compile-time safe as far as the value being passed to it if that's what you're asking.
Adam Robinson
@Adam: I am not doing design time binding because the datagridview is showing different object types depending on what view the user is set to. Maybe this is premature optimization and the over head of popping in and out different design time datagridviews isn't bad. Yeah it looks like compile-time safety for property names doesn't exist, it really doesn't seem like a difficult feature to add to the compiler.
Maslow
@Maslow: What you're asking to do is either have the compiler figure out (or somehow indicate to the compiler) that what you have as a string actually represents a property name. OK, so now it has to figure out what type they're supposed to correspond to (maybe simple in your scenario, but perhaps difficult or impossible in others). And that only allows for property names. What about objects that implement their own type descriptor, like a DataView? Those properties aren't actually created until runtime, so how is the compiler to detect those?
Adam Robinson