Reputation: 475
I have datagridview which displays it's data source (SQL database for an inventory system).
What I would like to be able to do is filter what is shown in the datagridview based on if something has been ordered etc...
So I am filling the datagridview like this:
private void Form1_Load(object sender, EventArgs e)
{
bG_T1TableAdapter.Fill(bUZZGEEF_DBDataSet.BG_T1);
ResizeCols();
}
but I can't for the life of me figure out how to query the dataset.
For instance I would like to click a button and that would display all rows in which the ordered column has been checked.
But in general I would like to know how to build my own queries.
Upvotes: 0
Views: 2156
Reputation: 20302
I think you want something like what you see in the code sample below. Basically, start typing in the TextBox, and the results in the GridView will be filtered, relative to what is entered into the TextBox.
using System.Data.SqlClient;
public class Form1
{
DataSet ds = new DataSet;
private void Form1_Load(System.Object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=name_of_your_sql_server; Integrated Security=true; Initial Catalog=name_of_your_database");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Categories", con);
da.Fill(ds, "Cat");
DataGridView1.DataSource = ds.Tables("Cat");
}
private void TextBox1_TextChanged(System.Object sender, System.EventArgs e)
{
ds.Tables("Cat").DefaultView.RowFilter = "[CategoryName] LIKE '*" + TextBox1.Text + "*'";
}
}
Upvotes: 0
Reputation: 159
You can use a DataView for the above purpose and bind the filtered DataView to the DataGrid.
bG_T1TableAdapter.Fill(bUZZGEEF_DBDataSet.BG_T1);
DataView dv = new DataView(bUZZGEEF_DBDataSet.BG_T1);
dv.RowFilter = "query"; // query example = "id = 10"
myDataGridView.DataSource =dv;
For more information, You can look here
Upvotes: 2
Reputation: 4156
You can use the DataTableView RowFilter to query a datatable
bG_T1TableAdapter.Fill(bUZZGEEF_DBDataSet.BG_T1);
bUZZGEEF_DBDataSet.BG_T1.DefaultView.RowFilter = "ID < 10";
myDataGridView.DataSource =bUZZGEEF_DBDataSet.BG_T1.DefaultView;
https://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter(v=vs.110).aspx
Upvotes: 1