-
Aug 25th, 2005, 08:44 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] populate a datagrid from subset of datatable records
Hi all.
I'm trying out the datagrid for the first time.
I've read a number of pages in Help about Datagrid source, Dataview, Datatable and others,
but I've been unable to achieve my basic goal.
I have a datatable "movies" with some basic columns Actors, Title, Year, Director, etc.
I want to populate a datagrid with just the fields Title and Year and only for movies with a certain actor, so I need to filter for rows and columns.
I have bound my table to the datagrid and it displays the entire set of records in the grid no problem. but i don't know how to proceed.
I tried using the table's select method, it works, and I set the table.select(Actors like '%somebody%') as the datasource for the datagrid and that does not raise an error in vb. but when it runs, the datagrid is only populated with column headers that say error
So, what is the right way to do this? I have been reading this forum and a couple others where people suggest hiding the unwanted columns. It doesn't seem like a healthy solution. Maybe I should just run a query and populate a new table with the desired columns and rows and bind that to the grid. is that too inefficient?
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 25th, 2005, 09:50 AM
#2
Frenzied Member
Re: populate a datagrid from subset of datatable records
filtering rows and filtering columns are two diefferent things that cannot be accomplished in the same time unless you requery the datatabase with what you need.
you can filter the columns by dropping or adding columnstyles in the datagridview.
you can filter rows by linking the datatable to a dataview and set the rowfilter of the datatview to your crieteria and then link the datagrid to the datatview and not the datatable.
this is the idea in genral
rgds
-
Aug 25th, 2005, 11:11 AM
#3
Thread Starter
Frenzied Member
Re: populate a datagrid from subset of datatable records
well, for the moment, I'm just populating a new datatable with the rows and columns I need and binding the datagrid to that table instead.
I guess it's solved for the moment.
I was stuck on how to implement columnstyles, though. After I set one up, I couldn't figure out how to apply it to the datagrid:
Dim GridStyle As DataGridTableStyle = New DataGridTableStyle
Dim TitleColumnStyle As DataGridTextBoxColumn = New DataGridTextBoxColumn
TitleColumnStyle.MappingName = "Title"
TitleColumnStyle.HeaderText = "Title"
TitleColumnStyle.Width = 120
GridStyle.GridColumnStyles.Add(TitleColumnStyle)
Grid.TableStyles.Add(GridStyle)
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 25th, 2005, 06:45 PM
#4
Re: populate a datagrid from subset of datatable records
There are a number of ways to achieve what you want depending on the situation. Assunimg you have retrieved the entire table using something like "SELECT * FROM Table1", you can bind your grid to the DataTable to show all the records. You can then set the DefaultView.RowFilter property of the DataTable to filter the rows displayed in the grid. As for columns, you can set the Width of certain columns to zero to hide them.
The other alternative, of course, is to use a different query, but generally you're going to want to keep the number of queries to a minimum.
-
Aug 25th, 2005, 08:10 PM
#5
Thread Starter
Frenzied Member
Re: populate a datagrid from subset of datatable records
jmc,
I'm going to ask this for the last time then. Are you saying that the common/popular way to deal with the undesired columns is to hide them? It seems like a shortcoming of the datagrid then.
and sadly, i still haven't figured out how to apply the column width or other properties of the column style (see code already posted). I think I'm still one line short.
I've been using the ComponentOne Flexgrid up to now. Have to say, it's more user friendly for what I've tried to do so far.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 25th, 2005, 08:18 PM
#6
Re: populate a datagrid from subset of datatable records
I've never used a DataGrid in my life, so all I know is what I've read. Having said that, I have read quite a bit, including point 5.13 on this page.
-
Aug 27th, 2005, 08:06 PM
#7
Thread Starter
Frenzied Member
Re: populate a datagrid from subset of datatable records
Hey JMC,
that page is loaded with good info. Solved all the problems I had with this datagrid.
Why can't MSDN be more like that?
I tried to add another point to your rep for this one but it wouldn't let me.
Appreciate it though.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|