Results 1 to 7 of 7

Thread: [RESOLVED] populate a datagrid from subset of datatable records

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,602

    Resolved [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

  2. #2
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    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

  3. #3

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,602

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  5. #5

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,602

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  7. #7

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,602

    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
  •  



Click Here to Expand Forum to Full Width