How to programmatically hide a linq-to-sql column on a datagridview? - linq

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.

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.

Couldn't you do it in the html? Or are you using ASP.NET?
<asp:BoundField DataField="id" Visible="false" />

Related

Why would I use a Kendo model instead of only using a transport/datasource

I am currently looking at Kendo UI and was wandering what is the "advantage" of defining a model in my schema. It seems to work quite well without. Is this for binding reasons (i.e. column discovery for Grid control for example) ? Does it allow particular validations ?
Thanks
Pat
You can use the schema model to specify your data model. On a grid, you can for example set:
Which data type the field has (if set to number, the column filter offers e.g. 'greater than, 'less than' instead of 'contains' etc. for string values).
If a grid field is editable or not (when grid is set to editable)
Several validation properties
...
I usually receive the grid data from a web service in JSON, so everything is a string initially. By using the model I influence how these values are displayed and how they behave.
See also
http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-schema.model
and http://docs.telerik.com/kendo-ui/api/javascript/data/model
for a full overview of possibilities.

Handling Relational Data Input

When creating an MVC application with a "Create" view for a particular entity and I want to relate it to another entity I could use a dynamic drop down menu.
However when the possible items is larger than 10 (for example) the drop down does not seem to offer the best user experience.
What is the recommended way to handle the input of a relationship between entities? A textbox that validates against the possible entities?
A textbox that validates against the possible entities?
That is pretty much the answer. The general idea would be to have a controller method that takes a query string and checks against the list of valid entities and returns the entities that match the query. The user can then choose from that filtered list.
You don't have to build it from scratch if you don't want to. Take a look at something like https://github.com/twitter/typeahead.js. There is also https://select2.github.io. However, there are probably lots of choices for that type of control.

Sorting by column in an XPages view data source

I have a dynamic view control (my own) which is fed by a configurable view data source. I need to be able to sort the view by various columns. Is there a way to do it with the view data source or do I need to roll my own? Thanks for the help.
For efficiency reasons, sorting should be handled by the domino runtime. The view must have the right columns either tagged as sorted or sortable by user. This creates the indexes within the NSF.
Then, the data source has some properties to control with sort index in being used, based on the name of the column you want to sort. The extlib DataView controls shows how this can be done in Java, through a JSF DataModel.
Also, you might consider using the DynamicViewpanel from the extlib/up1, as it does all of this for you.

Default Sort Column with Linq to SQL

I am in the process building myself a simple Linq to SQL repository pattern.
What I wanted to know is, is it possible to set a default sort column so I don't have to call orderby.
From what I have read I don't think it is and if this is the case what would recommend for a solution to this problem.
Would the best idea be to use an attribute on a partial class on my model?
the default order is the clustered index on the table you are pulling from.
What are you wanting to sort on (without sorting on) ?
If you needed something other than having it sorted by the primary key, you could look at supplying a select statement for the table instead of using the runtime generated statement. Look at the properties on the table in the designer -- you should be able to override the runtime generated select, delete, and update statements. I don't personally recommend this, though, since I'm not sure how it will interact with other orderings. I think the intent is more along the lines of allowing you to use stored procedures if you want.
Another alternative would be to create a table-valued function or stored procedure that does the ordering the way you want and has the same schema as the table. If, in the designer, you drag this onto the table, you get a strongly typed method on the data context that you can use to obtain those entities according to the definition of the function/procedure instead of the standard select. Personally I think this introduces fewer maintenance headaches because it makes it more visible, but you do have to remember to use the method instead of the Table property for that entity.

Linq to Entities, EntityReferences and DataGridViews

I am trying to select certain fields from my entity to be used as the datasource for a datagridview, but I haven't been able to make it work. Is such a thing possible? For example, I have a Customers entity that contains several entityreferences. I want to take fields from the customers entity and from within those entityreferences and display them in the datagridview. I haven't been able to come up with a Linq query to accomplish this, and even when you simply use the entire entity as the datasource the fields within the entityreferences are not displayed. Any idea what I am doing wrong? Thanks for the help.
from customer in context.customers
select new
{
Name = customer.Name,
City = customer.Address.City
}
that will create a custom object and you can see the second property is referencing an entity field on the primary entity.. basically just transform the data to a new object and bind the enumerable generated to the grid.
sorry if this is a little mumbled, typing on my phone.
Caveat: This is not tested with entity framework references.
When using object data sources you can reference properties of object references, but you must first cast the object:
<asp:Label ID="lblCity" runat="server" Text='<%# ((Customer)Container.DataItem).Address.City%>'></asp:Label>
Could this be your problem accessing properties of the entity references?

Resources