Deal of the Day

Home » Main » Manning Forums » 2007 » LINQ in Action

Thread: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 10 - Pages: 1 - Last Post: Sep 10, 2009 9:22 AM by: fabrice.marguerie
Scott_H_Chang

Posts: 6
From: Baltimore, MD
Registered: 9/3/09
Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 3, 2009 2:43 PM
  Click to reply to this thread Reply

Hi,

I tried to execute Listing 4.13 in my Visual C# 2008 Express. I created a Windows Forms application, dropped a DataGridView control on the Form1 and typed the following code to Form1.cs:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace scWinFormLINQinAction4_13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
String[] books = { "Funny Stories",
"All your your base are belong to us", "LINQ rules",
"C# on Rails", "Bonjour mon Amour"};

var query =
from book in books
where books.Length > 10
orderby book
select new { Book = book.ToUpper() };

dataGridView1.DataSource = query.ToList();
}
}
}



I executed this project. It ran and I got the result with output of "Book" in the first row of the DataGridView1 only and no output for the code statement "dataGridView1.DataSource = query.ToList();". I guess I missed to do somethings in the Properties of DataGridView1!!?? Please help and advise me what I missed in this project and how to correct the problem.

Thanks,
Scott Chang

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 3, 2009 6:16 PM   in response to: Scott_H_Chang in response to: Scott_H_Chang
  Click to reply to this thread Reply

Hi Scott,

What you see is normal.
In your code, query.ToList() returns 0 elements.

This happens because you made a typo in the query. I let you find it. Hint: the condition in where is always false...
Do you see your mistake now? :-)

Fabrice

Scott_H_Chang

Posts: 6
From: Baltimore, MD
Registered: 9/3/09
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 4, 2009 7:31 AM   in response to: fabrice.marguerie in response to: fabrice.marguerie
  Click to reply to this thread Reply

Hi Fabrice, Thanks for your response.

I got it - I changed "books" to "book" after the where and before the .Length >10.
I ran it and it worked nicely as your book shows.

I want to go on to do the "Step 2: Using richer collections" to get the "Book" and "Price" result of Figure 4.12 Result of the second Windows Forms step. I read Pages 135-137 (for Windows Forms) and Pages129-132 (for aspx.cs) of your book and I still can not figure out how to add the LINQ code statements for the "Price" associated with the "Book"!!?? I feel ASP.NET is harder than Windows Forms and I have not tried to learn ASP.NET not all. Could you please help and enlighten me in doing this Step 2 (in Windows Forms)? Please respond.

Thanks again,
Scott Chang

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 4, 2009 8:47 AM   in response to: Scott_H_Chang in response to: Scott_H_Chang
  Click to reply to this thread Reply

Here is the query you should use:

from book in SampleData.Books
where book.Price >= 15
select book

SampleData is a class you can use by referencing the LinqBooks.Common project. This is explained just at the beginning of Step 2, on page 135.
The query is not shown in the book though.

When in doubt, you can refer to the complete source code that we've made available here: http://linqinaction.net/files/folders/linqinaction/entry1952.aspx
See FormBooks.cs for this particular case.

Fabrice

Scott_H_Chang

Posts: 6
From: Baltimore, MD
Registered: 9/3/09
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 4, 2009 2:23 PM   in response to: fabrice.marguerie in response to: fabrice.marguerie
  Click to reply to this thread Reply

Hi Fabrice,
I downloaded the complete source code from the Link you gave me.
I used my Visual C# 2008 Express to open the LingBooks.Common.csproj and I got the following 15 errors:
Error 1 The type or namespace name 'Publisher' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 11 11 LinqBooks.Common
Error 2 The type or namespace name 'Publisher' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 12 11 LinqBooks.Common
Error 3 The type or namespace name 'Publisher' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 13 11 LinqBooks.Common
Error 4 The type or namespace name 'Author' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 18 11 LinqBooks.Common
Error 5 The type or namespace name 'Author' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 19 11 LinqBooks.Common
Error 6 The type or namespace name 'Author' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 20 11 LinqBooks.Common
Error 7 The type or namespace name 'Author' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 21 11 LinqBooks.Common
Error 8 The type or namespace name 'Subject' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 26 11 LinqBooks.Common
Error 9 The type or namespace name 'Subject' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 27 11 LinqBooks.Common
Error 10 The type or namespace name 'Subject' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 28 11 LinqBooks.Common
Error 11 The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 33 11 LinqBooks.Common
Error 12 The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 43 11 LinqBooks.Common
Error 13 The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 53 11 LinqBooks.Common
Error 14 The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 63 11 LinqBooks.Common
Error 15 The type or namespace name 'Book' could not be found (are you missing a using directive or an assembly reference?) C:\C#-LINQinAction\SampleData.cs 73 11 LinqBooks.Common

I spent several hours to look into these errors and I have no ideas why I got them!!?? Could you please help and advise me how I can resolve these errors?

Thanks,
Scott Chang

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 8, 2009 6:08 AM   in response to: Scott_H_Chang in response to: Scott_H_Chang
  Click to reply to this thread Reply

I've never used Visual C# Express, so, I don't really know what happens here.
All I can tell is that all the missing types (Publisher, Author, Subject and Book) are really declared in LinqBooks.Common.csproj.
If you look at the csproj file, you'll see the following:

    <Compile Include="BusinessObjects\Book.cs" />
<Compile Include="BusinessObjects\Author.cs" />
<Compile Include="BusinessObjects\Review.cs" />
<Compile Include="BusinessObjects\Subject.cs" />
<Compile Include="BusinessObjects\Publisher.cs" />
<Compile Include="BusinessObjects\User.cs" />

Do you see the files in the BusinessObjects folder? Have you tried to include them again in the project?
The Express editions of Visual Studio seem to be strange.

Fabrice

Scott_H_Chang

Posts: 6
From: Baltimore, MD
Registered: 9/3/09
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 8, 2009 7:16 AM   in response to: fabrice.marguerie in response to: fabrice.marguerie
  Click to reply to this thread Reply

Hi Fabrice,

The BusinessObjects folder I downloaded and saved in my C:// drive is empty - I think the computer system of my office does not allow the .zip files to pass through our computer system for the security reason. Could you please give me the non-zipped files "Book.cs", "Author.cs", "Review.cs", "Subject.cs", "Publisher.cs" and "User.cs" as well as the necessary "BusinessObjects" stuff associated the above-mentioned 6 files? My e-mail address is scott.h.chang@usace.army.mil.

Scott_H_Chang

Posts: 6
From: Baltimore, MD
Registered: 9/3/09
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 9, 2009 1:42 PM   in response to: Scott_H_Chang in response to: Scott_H_Chang
  Click to reply to this thread Reply

Hi Fabrice,

I have struggled to get the LinqBooks.Common project installed in my Visual C# 2008 Express desperately. I feel that I need to create a Windows Form project for LinqBooks.Common from the scratch in my Visual C# 2008 Express myself - I should add the BusinessObjects that has Authors.cs, Book.cs, ClassDiagram1.cd, Publisher.cs, Review.cs, Subject.cs and User.cs. I saw the complete code statements of SampleData.cs from your downloaded files. But I do not know the contents/code statements of the BusinessObjects that has Authors.cs, Book.cs, ClassDiagram1.cd, Publisher.cs, Review.cs, Subject.cs and User.cs. Could you please give me the detailed instructions of writing these .cs and .cd files? By the way, what is ClassDiagram1.cd? I really want to pick up the skills of Windows Form-DataGridView (Book & Price) LINQ in C# Programming. Please kindly help me and respond ASAP. Many Thanks, Scott Chang

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 9, 2009 3:27 PM   in response to: Scott_H_Chang in response to: Scott_H_Chang
  Click to reply to this thread Reply

Scott,

Didn't you get the files in the e-mail I sent you yesterday?
The Linqbooks.Common project is not a Windows Forms application, but simply a class library.

Fabrice

Scott_H_Chang

Posts: 6
From: Baltimore, MD
Registered: 9/3/09
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 10, 2009 9:13 AM   in response to: fabrice.marguerie in response to: fabrice.marguerie
  Click to reply to this thread Reply

Hi Fabrice, Thanks for sending me the files.

I am studying the files you just gave me, the Linqbooks.Common Project, the class library, SampleData.Books, etc....I feel I do not know how to reference the LinqBooks.Common (on Page 135) you mentioned in your Sept 4, 2009 post. Could you please give me more instructions on the "reference" thing?

Thanks again,
Scott Chang

fabrice.marguerie


Posts: 225
From: France
Registered: 4/28/06
Re: Doing Windows Form-DataGridView LINQ in Visual C# 2008 Express: output has
Posted: Sep 10, 2009 9:22 AM   in response to: Scott_H_Chang in response to: Scott_H_Chang
  Click to reply to this thread Reply

Sorry Scott, but this is basic .NET programming. That's why this kind of things is not explained in the book.
You can add a reference to a project or an assembly using the "Add Reference..." command in a project's contextual menu.

You first need to understand .NET and what references are before being able to learn LINQ. Sorry again, but LINQ is a part of .NET, and so you first need to know the .NET basis before using LINQ.

Fabrice

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor