1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

How to search a data source and output in data grid view

Discussion in 'Programming & Webmastering' started by Akumos, May 10, 2010.

  1. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    552 (0.18/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    Hi...

    How do I search a datasource and output the results using a data grid view?

    So far, I have it displaying all the records in the database...
     
  2. temp02 New Member

    Joined:
    Mar 18, 2009
    Messages:
    493 (0.17/day)
    Thanks Received:
    166
    In what Programming language and/or IDE?
     
  3. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    552 (0.18/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    whopps, it's VB2005!
     
  4. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,931 (1.12/day)
    Thanks Received:
    933
    Location:
    Geneva, FL, USA
    Generally you try to only query data that you're going to use, for instance:

    SELECT first_name, last_name FROM users WHERE gender = "m"


    If you need to search for something in a field,

    SELECT first_name, last_name FROM users WHERE first_name LIKE "fred%"

    That would find Fred, Frederick, Frederica, etc.


    There is *much* more to it (full text indexes, other functions, modifiers) and while most databases have similar functionality some of it is implemented in different ways. Which one are you using?
     
    Crunching for Team TPU
  5. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    552 (0.18/day)
    Thanks Received:
    28
    Location:
    Birmingham, England...
    ok cool

    where do I put the SQL statement when I have a datagridview of the data source?
     
  6. temp02 New Member

    Joined:
    Mar 18, 2009
    Messages:
    493 (0.17/day)
    Thanks Received:
    166
    It has been a while since I've messed with VB, so here (Martin Xie post) it is a proper example either for a MS Access DB (.MDB) and an SQL Server DB (.MDF).
     
  7. doallen194 New Member

    Joined:
    Jun 1, 2010
    Messages:
    3 (0.00/day)
    Thanks Received:
    0
    Re : How to search a data source and output in data grid view

    Thanks for great reply and i have query where i put this statement
     
  8. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,826 (3.73/day)
    Thanks Received:
    5,631
    Location:
    Cheeseland (Wisconsin, USA)
    The easiest way is to create a DataAdapter, DataSet and Binding source, then bind the DataGridView to the BindingSource.

    In C# it's something like this ...
    Code:
    DataSet D = new DataSet();
    SqlDataAdapter A = new SqlDataAdapter(query, connection);
    A.Fill(D, "Table Name");
    BindingSource BS = new BindingSource(D, "Table Name");
    
    myDataGridView.DataSource = BS;
    
    Then if you want to change the data that is in the DataSet ..
    Code:
    D.Clear();
    A.SelectCommand.CommandText = NewQuery;
    A.Fill(D, "Table Name");
    

    I can give you exact syntax if you need it.
    It should be an easy port to VB.
     
    10 Year Member at TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)