Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Folks,

In my current project I'm using LINQ to SQL with an SQLite database, using VS2008 and c#. I wanted a form to display the contents of a table, so set up a DataGridView to do that. Unfortunately I can't get anything displayed - the DGV remains blank.

All the articles I've seen suggest the following should work, though it can also be done with a BindingSource:

C#
db = new RWInfoDB(dbConnection);
var rwInfoTableQuery = from d in db.GetTable<RWInfoTable>() orderby d.Id select d;
pkgView.Show();
int c = rwInfoTableQuery.Count();
pkgView.dataGrid.DataSource = rwInfoTableQuery;


Where RWInfoDB is my DataContext-derived class, dbConnection returns a pre-canned SQLite connection object, pkgView is the form the DGV is on, and the "int c" line just proves the query works.

Inspecting the DGV object at runtime shows a DataSource member with visible data - I can see the rows in there - and yet the DGV has 0 in Columns and Rows - as if it hasn't even looked at the DataSource.

[I started with Show() called after setting DataSource, but moved it to see if that would help]

Edit: Are there properties in the DGV that could stop it working in this way?

Any thoughts?

Ruth
Posted
Updated 2-Jun-11 10:55am
v3

To read a table in LINQ you have a variety of options.

The simplest would be to drag the table into the DBML design and use the following code:

Using (Mydatacontext mydc = new Mydatacontext ())
{
    List<mytable> rows = mydc.mytableresults.tolist();
    mydatasource= rows;
}
</mytable>

Another option would be to create a stored procedure and read the results in the same way as above to populate your grid. this method works much better when your queries become more complex
 
Share this answer
 
Comments
Ruth Ivimey-Cook 7-Jun-11 19:28pm    
Sorry I didn't respond straight away - stuff got in the way.
So far I'm trying to avoid the DBML style methods as it introduces yet another set of unknowns for me.

As far as I can tell from your code snippet, you are saying that the examples (including those by MS) need the ToList<>() function applied for the DataContext, rather than supplying the DataContext itself as the DataSource?

I was looking earlier at articles such as:
www.c-sharpcorner.com/.../dbindlinq.aspx

I've just tried again using a BindingSource. Create the object, assign my IOrderableQuery<> to it's data source, then assign the binding to the DGV's DataSource. Again, I can see after all this that the BindingSource object has opened the query and loaded the first record, quite correctly, but the DGV has no columns, no rows.

I next tried uncommenting all but essential (size etc) setup in the xx.Designer.cs file; makes no difference. I've checked that the AutoGenerateColumns is set true.

Very confusing.

Thanks, Ruth
milenalukic 8-Jun-11 8:05am    
have a look in the DGV Tasks - the little black arrow on the topright of grid. Make sure the datasource is set and that the columns are included.
Sorted it!

I discovered in the MS docs a "throwaway" comment that "you must use properties" - what for, it didn't say.

However, although several things in LINQ worked fine with a field-based class for my table row object (the one tagged [Table] with [Column]'s within it), DGV doesn't. Make automatic properties of all the fields and the DGV works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900