Results 1 to 13 of 13

Thread: [RESOLVED] Filtering

  1. #1

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Resolved [RESOLVED] Filtering

    Hi again everyone.

    I'm using Microsoft Access (2003), Visual C# .Net, and an OleDbConnection.

    I've been filtering a data grid view by means of a Fill Command:

    Code:
    SELECT     ID, [Column 2], [Column 3], [Column 4]
    FROM         [Table 3]
    WHERE     ([Column 2] = ?)
    By simply calling this every time Column 2 changes, the datagrid view continually displays only the records that are relevant to the current object (which is denoted in colmn 2, obviously).

    I now need to make it possible to select (and display) several objects at once (still filtering by column 2, but where Column 2 is equal to any item in the specified List<>. The list can be any size).

    Is there a way to retrieve this? Or a better way of doing things?

    Thanks in advance.
    Qu.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Filtering

    See if access allows you to use 'in'
    Code:
    SELECT     ID, [Column 2], [Column 3], [Column 4]
    FROM         [Table 3]
    WHERE     ([Column 2] in ('thing1','thing2'))

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filtering

    See the link in my signature about using parameters with an IN clause.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Filtering

    Thanks guys: the IN statement is what I need, but I've come across another problem. I've been googling various SQL references all morning, and haven't been able to solve it even though it should be really simple.

    I'm using the designer to generate my Fill statement, and have eventually come up with this:
    Code:
    SELECT     ID, [RFI #], [Area/Seq], [Requested By], etc...
    FROM         RFI
    WHERE     (ID IN (?))
    ID is an autonumber field.

    The method generated is:
    Code:
    TableAdapter.FillByIDNum(DataTable Table, int Param1);
    Having gathered a list of integers, I need to display only those whose ID matches an integer in the list. Unfortionately, I can't feed in a list of integers into a field that takes only 1 integer, and I can't change the type to string because then I get a dataTypeMismatch.

    Does anyone know the way around this? I'm at my wits end.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filtering

    You can't use the designer if your query changes at run time. Did you read the link I mentioned?
    You cannot use a single parameter for the whole list, but if you don't know how many values will be in the list then you can't add the parameters easily.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Filtering

    Yes, I did read your link, and at the time it gave me what I thought I needed: the ability to use the IN statement.

    I don't really want to have to rebuild the entire system, because I've already done a lot of work in the designer. Hmmm... can I define an SqlCommand (as you do in your link) and somehow plug it straight into a TableAdapter?

    Dealing with access makes me feel so ignorant.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filtering

    A TableAdapter is a wrapper for a DataAdapter and a connection. The TableAdapter has a SelectCommand property, which is a pass-through for the internal DataAdapter's SelectCommand property. You could, if you really wanted to, set the CommandText of that Command at run time. I've never tried before but I'm fairly sure it's public as you can access it in the Properties window.

    That said, it's easy enough to create a DataAdapter and use it to Fill the same DataTable you would have with your TableAdapter.
    Last edited by jmcilhinney; Jul 16th, 2008 at 12:05 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Filtering

    Apparently those variables are not public, so I tried to create a DataAdapter. Never having done it before, I must have screwed somthing up

    It doesn't seem to make any difference to the Datagridview.

    Here's my code:
    C# Code:
    1. string MainSQLFilter = @"SELECT     ID, [RFI #], [Area/Seq] //etc...
    2. FROM         RFI
    3. WHERE     (ID IN (";
    4.             //Builds the ID List using string concatenation. I know it's bad practice, but this is only for testing.
    5.             for (i = 0; i < idNumbers.Count; i++ )
    6.             {
    7.                MainSQLFilter += idNumbers[i].ToString();
    8.                 if (i < idNumbers.Count - 1)
    9.                 {
    10.                     MainSQLFilter += ", ";
    11.                 }
    12.                 else
    13.                 {
    14.                     MainSQLFilter += "))";
    15.                 }
    16.             }
    17.             //Both of these seem to return correctly
    18.             MessageBox.Show(MainSQLFilter + "\r\n" + connectionString);
    19.             //And here I make a DataAdapter
    20.             OleDbDataAdapter da = new OleDbDataAdapter(MainSQLFilter, connectionString);
    21.             //And use it to fill the data table
    22.             da.Fill(virtualModelDataSet.RFI);

    But my DataGridView doesn't change. Any idea why?
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filtering

    What does Fill return? Is that DataTable bound to your grid?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Filtering

    Fill returns a 2, which I believe is correct: the list I'm feeding in consists of 5,6,28 and 30, and only 5 and 6 appear in the ID column.

    The DatagridView's DataSource is a Binding Source, which has the Table as it's DataMember property and the DataSet as it's DataSource property.
    This is the setup automatically generated by the designer.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  11. #11

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Filtering

    Bump.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Filtering

    If your DataGridView is bound to a BindingSource then the grid will reflect whatever data is exposed by that BindingSource. If the BindingSource is bound to a DataTable then the BindingSource will expose whatever data is in the DataTable, depending on its Sort and Filter properties. If you are adding new rows to a DataTable when you call Fill and no new rows show up in your grdi then there are only two possibilities that I can think of:

    1. The table is not bound to the grid.
    2. The Filter property of the BindingSource prevents those new records being displayed.

    First up, I think you'll find that, by default, calling Fill on a DataAdapter will not clear the existing data from the DataTable. Have you tested the number of rows in the DataTable before and after you call Fill to be sure the number is changing? Are you using the debugger to view the data in the DataTable before and after calling Fill? Can you confirm that the BindingSource isn't filtering the data out?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member Quasar6's Avatar
    Join Date
    Mar 2008
    Location
    Sol 3
    Posts
    325

    Re: Filtering

    Really REALLY big thanks! This:
    First up, I think you'll find that, by default, calling Fill on a DataAdapter will not clear the existing data from the DataTable.
    Made me realise all I needed to do was call Clear() on my dataTable before filtering!

    Again, big thanks! Thread resolved.
    "Why do all my attempts at science end with me getting punched by batman?" xkcd.

    |Pong||
    Sorry for not posting more often.

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