The Way to Programming
The Way to Programming
I’m currently working on an assignment for school, which involves having to make an application that retrieves data from a database and shows that data on the screen. Now I am making a kind of inventory for products and etc, and you have 3 possibilities: You can choose to search on ProductId, CustomerId and OrderId.
How can I load a different datagridview based upon what the person has entered? I already have the code to figure out what they filled in, I simply need to know how I can manually load a datagridview on my form and add data into it.
Use only one DataGridView and then use Ling to filter out the results. If you don’t need to edit/modify the results, it will be a bit easy Take a look at this link below. Basically you change the DataGridView source based on the query. So ProductId, CustomerId, and OrderId would pull from the same database, but you would use a filter to set each datasource. Let’s say you have a ComboBox you can just have the DataGridView.DataSource change depending on the ComboBox selection.
If I have time tomorrow, I’ll put together a working sample, just let me know if you need to edit the results, because that’s more involved.
http://stackoverflow.com/questions/1000801/simplest-way-to-use-a-datagridview-with-linq-to-sql
Take a look at this for more Linq queries.
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
Here you go. I just tested this an it worked. Use it for reference. I didn’t have a data source so I created a test class. Could be a little cleaner, but I’m pretty beat. Should add a catch for null, but this should get you in the right direction. Just use the values I put in under cust1,2, and 3 to search.
Add these to the form:
DataGridView named dgvCustomers, TextBox called tbSearch ComboBox called cbFilter Button called btnSearch
Then add Customers as your DataSource in properties for the DataGridView
public partial class Form1 : Form { private ListcustomerList; public Form1() { InitializeComponent(); InitializeCustomers(); } public class Customers { private string _customerName; private string _productID; private string _customerID; private string _orderID; public string CustomerName { get { return _customerName; } set { _customerName = value; } } public string ProductID { get { return _productID; } set { _productID = value; } } public string CustomerID { get { return _customerID; } set { _customerID = value; } } public string OrderID { get { return _orderID; } set { _orderID = value; } } } private void InitializeCustomers() { Customers cust1 = new Customers(); cust1.CustomerName = "Jack Johnson"; cust1.ProductID = "p111"; cust1.CustomerID = "c111"; cust1.OrderID = "o111"; Customers cust2 = new Customers(); cust2.CustomerName = "John Smith"; cust2.ProductID = "p222"; cust2.CustomerID = "c222"; cust2.OrderID = "o222"; Customers cust3 = new Customers(); cust3.CustomerName = "Joe Johnson"; cust3.ProductID = "p333"; cust3.CustomerID = "c333"; cust3.OrderID = "o333"; List cList = new List (); cList.Add(cust1); cList.Add(cust2); cList.Add(cust3); customerList = cList; dgvCustomers.DataSource = customerList; this.cbFilter.DisplayMember = "Text"; this.cbFilter.ValueMember = "Value"; var items = new[] { new { Text = "ProductID", Value = "ProductID" }, new { Text = "CustomerID", Value = "CustomerID" }, new { Text = "OrderID", Value = "OrderID" }, }; this.cbFilter.DataSource = items; } private void btnSearch_Click(object sender, EventArgs e) { var pQuery = from cust in customerList where cust.ProductID == tbSearch.Text select cust; var oQuery = from cust in customerList where cust.OrderID == tbSearch.Text select cust; var cQuery = from cust in customerList where cust.CustomerID == tbSearch.Text select cust; string cbValue = cbFilter.SelectedValue.ToString(); switch (cbValue) { case "CustomerID": dgvCustomers.DataSource = cQuery.ToList(); break; case "ProductID": dgvCustomers.DataSource = pQuery.ToList(); break; case "OrderID": dgvCustomers.DataSource = oQuery.ToList(); break; } } }
Sign in to your account