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



## Akumos (May 10, 2010)

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...


----------



## temp02 (May 10, 2010)

In what Programming language and/or IDE?


----------



## Akumos (May 10, 2010)

whopps, it's VB2005!


----------



## Disparia (May 10, 2010)

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?


----------



## Akumos (May 10, 2010)

ok cool

where do I put the SQL statement when I have a datagridview of the data source?


----------



## temp02 (May 10, 2010)

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).


----------



## doallen194 (Jun 3, 2010)

*Re :  How to search a data source and output in data grid view*



Jizzler said:


> 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"
> 
> ...



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


----------



## Kreij (Jun 3, 2010)

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 ...

```
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 ..

```
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.


----------

