Linq to Sql Not giving proper data
This is very simple linq and I am just beginning with it. I have traced the problem down to one of 3 sources but am not sure how to fix it. So here is the code, and I will explain the problem below it.
XAML Binding:
<ListBox Name="lstJobs" ItemsSource="{Binding Path=Title}" DockPanel.Dock="Top"
MinWidth="150" MinHeight="200" MaxHeight="250"
ScrollViewer.VerticalScrollBarVisibility="Visible"
SelectionChanged="lstJobs_SelectionChanged" />
C# Code Behind (Window loaded event):
TMDataContext TMDataBase = new TMDataContext();
lstJobs.ItemsSource = from j in TMDataBase.JobDetails
select j;
Result:
Problem: I know the connection is working because there are only 3 records in the JobDtail table.
Back in college we learned that classes output their name if the ToString method is not overloaded to force it to do something else. The problem is that I dont want to modify the classes generated by L2S because the开发者_运维问答y are automatically generated and any modifications will be lost later. I will be making changes so this and making changes to auto generated code is redundant.
Before doing the L2S, I had the database built, and a bunch of "entity" classes all pre designed using inheritance which I had to delete because they caused conflicts with the L2S classes. The L2S autogenerated classes are far to meessy for me and I would like to be able to replace their properties, constructors, and add methods. Or at least rename or extend them somewhere neater where I can re implement my inheritance structure.
The methodology I am using is simple. A set of classes for the UI. A set of "Control" classes to handle the business logic, and a set of "Entity" classes to interface with the DB. But the whole L2S automated class generation is getting in my way of this by replacing my entity classes.
Anyway, My first problem is I want to display the Job Title (one field form the Job Details table) in the list box for each record. When the selection is changed in the listbox, the relevant record will be accessed and will fill in a bunch of text boxes that will allow the user to edit that record.
Any help with this would be really appreicated. Thanks in advance.
Your C# code behind is overriding your XAML binding. Currently it is just calling ToString on your objects because it doesn't know which propery to display. In your XAML, you either need to specify the DisplayMemberPath/ValueMemberPath or use a DataTemplate, or in your LINQ just project a list of strings:
lstJobs.ItemsSource = from j in TMDataBase.JobDetails
select j.Title;
LinqToSql generates partial classes. You can supply the other part and it won't be wiped out when the dbml designer regenerates.
This allows you to add a .ToString to those classes if you like.
Or, you can change how you bind the data to the UI control.
精彩评论