Filtring/Searching from a DataGridView - sql

I have a DataGridView in a form which displays a client list with the columns ID, First Name, Last Name, Address. I also included a TextBox to perform the search query. I want to filter my DataGridView based from the given columns using a single TextBox (like a multi data filter/search where I can select from those four columns by typing on a single TextBox).
The question is: am I required to create a binding source for here (I populate my DataGridView using sql database) or is there a way to create filters without having to add a binding source?

You don't populate your grid using a sql database. You populate your grid from a SQL Server database suing something else in between, e.g. a SqlDataAdapter and a DataTable.
A BindingSource is supposed to be a one-stop shop for working with bound data so, while you don't have to use one, I would recommend doing so. Whether it can help you filter your data depends on what it's bound to. The BindingSource doesn't actually do the work of filtering itself but rather passes the work on to the underlying IBindingListView implementation if there is one. For instance, if the underlying data source is a DataTable then the RowFilter of its DefaultView will be used. You could set the DefaultView.RowFilter yourself.
If you want to do all the heavy lifting yourself then you could also search the grid yourself and then hide the rows that don't match. Those rows would still exist though, so you'd have to take that into account when using the data in code.

You have to create a query to retrive a data using like keyword by passing parameter value in form
Example:
pass the parameter from
form(cmd.parameters.add(nvarchar, 20).value = "textbox.text")
create a stored procedure
CREATE PROCEDURE procedure_name
#searchvalue varchar(20)
AS
BEGIN
SELECT
ID, FirstName, LastName, Address
FROM
tablename
WHERE
FirstName LIKE '%#SearchTerm%'
END
This above stored procedure will retrieve all the rows where the FirstName column value corresponds to that name
Then pass the result to gridview

Related

selecting a column values from a Table using LINQ (Entity Framework)

Im trying to display a dropdown with values from table 'Events'. I have created a Model.edmx which has the structure of the database. Now i have to just write LINQ code to display one column values. I am kind of new to LINQ.
Dim Events=" LINQ select statement part???"
ddlEvent.DataSource = Events
ddlEvent.DataBind()
ddlEvent.Items.Insert(0, New ListItem("-Type-", ""))
DropDownLists work with 2 fields:
the DataTextField represents the field holding the text of the options that will be displayed to the user
the DataValueField represents the field hoding the value associated with each option
So in your case, you could get it to work with something like:
Dim events As List(Of Event) = yourDbContext.Events.ToList()
ddlEvent.DataSource = events
ddlEvent.DataTextField = "NameOfThePropertyYouWantDisplayed"
ddlEvent.DataValueField = "NameOfThePropertyYouWantAsValue"
ddlEvent.DataBind()
Note, however, that the query will retrieve every property from the database. This can hurt performance if your Event class has a lot of properties.
To avoid this, you could use the LINQ Select operator, used for projection. This could be used to retrieve only the necesseray data from the database. You can have a look on the MSDN here
Context.Events.ToList();
Where, Context is your EF db context. Update your filter condition in where clause
ddlEvent.DataSource = from x in Context.Events select x.column;
here 'x' is a variable and 'column' in 'x.column' is required column you want to fetch.
Where, Context is your EF db context.

Store Input Box Data Temporally in VB.net

I'm trying to create a Windows Form application which will prompt for data to then update a SQL table.
The SQL table requires 3 fields, column A, column B, column C.
To make it easier for the end user, I'm trying to allow them to input all the data in for column C first (As they won't know the relevant data for Columns A and B until the end of the process when they request that data from a third party).
Is there a way to store all the data they add for column C, then at the end request a single piece of information for A and B, which will then populate the other two columns and update the SQL table with these rows?
Thanks
Sounds like you could use a datagridview with ADO.net. Make the datagrid 3 columns wide, and utilize the first column (does it make more sense to fill out the first column rather than third? You can update it back to the database in any order.)to expect the value that you want to capture (you can add more datagrid rows as needed). In order to do this query to the database, you can add a database through dataset designer and create a method to pull/update data to/from sql server.
To get data out of the DB, you could loop through each row of the datagridview, collect the column1 item, pass it as a parameter (if needed) to your new method, receive the data back from the database, fill the the other two columns in the row.
Now that you have that data you could do an entire update into the database using another method created in the dataset designer.

asp.net merging result of 2 database search in one gridview

I have 2 databases from 2 third party providers.
both database have a table with a common unique identifiers (UI).
I want to display table1 of DB1 filters out by the result of a list of unique Identifier retrieved for DB2.
and display the result in gridview.
the initial idea was to insert db2 UI in a virtual table in DB1 and then the usual select db1.table1 - JOIN Virtualtable or where in. but this would be a very slow process considering the search happen with every page post back.
Would it be possible to filter out the result in code behind during gridview.databinding?
or its datasource databinding?
I found a solution:
I Don't use the mark up page Datasource, I use a DataTable in code behind.
Fill datatable with a standard sqlcommand and sqldatareader.
Then while datareader.read I skip rows for which UI does'nt match list of "good" UI from DB2.
Then Bind GridView to Data table.
It even works with gridview paging which i was not expecting to.

How can I bind a multi-row update or insert statement to a form?

I have a table Prices:
ID -- primary key, autonumber long integer
PriceDate -- Date
Price - Currency
Quantity - Number, DECIMAL subtype
UnitPrice - Number, DECIMAL subtype (an update statement is run to keep this in synch with price and quantity, but it's just a convenience for indexing... probably it'll be replaced with an expression in my queries)
ItemNote - Text
NewStores_ID - long integer key, lookup to another table of stores
NewItems_ID - long integer key, lookup to another table of items
To enter prices for a given store on a given day, I would like to be able to select the store and date ONCE on a form, then enter the items individually in a datasheet. For reasons unexplained, this proves difficult.
I can create a subform binding everything but the store and price to a temp table TempPrices with the same structure as the original. Then I run the SQL statement
INSERT INTO Prices
(PriceDate,Price,Quantity,UnitPrice,Brand,ItemNote,NewStores_ID,NewItems_ID)
SELECT
PriceDate,Price,Quantity,Price/Quantity AS
UnitPrice,Brand,ItemNote,NewStores_ID,NewItems_ID)
FROM Temp_Prices;
This will feed all the new rows into the main table. But, when I want to set the store and date only once, I run into problems. I've tried using named parameters for date an store in the insert statement... which can cause a pop-up prompt, but I cannot bind it to a form control. I've tried binding an update statement for those fields in the temp table to a form... but it doesn't even show an option to bind a multi-row update.
How can I get this to work with a minimum of clumsy hackery? It seems like there ought to be a simple solution, and if I were using something like PHP or JDBC I'd just run an extra query.
Edit: changed storage type for Quantity and UnitPrice to Number, Decimal subtype in place of double float. Just so people won't cry about using a float in any proximity to currency. It doesn't pose a problem in my use, but there are enough people who have a knee-jerk reaction to that.
Edit 2: Form/Subform
I'm trying to structure this as a master form with a either fields for entering store name and date, or a subform for the same, then a subform mapping to the temporary table for entering pricing data. There is an action button to run the insert/update queries to dump the temp table into my main prices table and clear out the temp table. However, the problem is that I can't figure out how to get the date/store fields in the master (or subform) to bind to an insert/update value applied to all the new rows at once.
Edit 3: SQL Statements (for clarity)
INSERT INTO
PRICES(NewStores_ID,PriceDate,NewItems_ID,Brand,Price,Quantity,
UnitPrice,ItemNote)
SELECT
#MyStore_ID,#MyPriceDate,NewItems_ID,Brand,Price,Quantity,
Price/Quantity,ItemNote
FROM TempPrices;
UPDATE TempPrices SET PriceDate=#MyPriceDate,NewStores_ID=#MyStoreID;
For these queries, I cannot bind parameters for #MyStore_ID or #MyPriceDate to fields in any form. The queries don't show up as options when trying to link them to a form. I can run them and get popup boxes to input parameters, but that's not what I want.
This is the target I'm aiming at:
"I would like to be able to select the store and date ONCE on a form, then enter the items individually in a datasheet."
However, if you have a reason why you need to do it with a temp table and DML statements, then this suggestion will not be useful.
I created a Prices table with only 4 fields, then a query on that table which I used as the Record Source for a form, "fsubPrices":
SELECT p.ID, p.NewStores_ID, p.PriceDate, p.Price
FROM Prices AS p
ORDER BY p.NewStores_ID, p.PriceDate;
The form has text boxes (txtNewStores_ID, txtPriceDate, and txtPrice) bound to the similarly-named query fields. I set Enabled=Yes for txtPrice, and Enabled=No for the other two.
Then I created an unbound form "frmPrices", and in the form header added a combo box "cboStores" and a text box "txtPriceDate". The combo has Bound Column = 1 with this query for its Row Source:
SELECT l.Store_ID, l.Store_name FROM tblkupStores AS l ORDER BY l.Store_name;
Then I added fsubPrices as a subform control to the detail section of frmPrices. The tricky part is setting the Link Master/Child Fields. There is a "wizardy dialog thing", but it will only allow you to select from the available fields, and I needed to link controls on the main form with fields on the subform. To do that, I had to type what I wanted directly into the subform control's property sheet:
Link Child Fields......NewStores_ID;PriceDate
Link Master Fields.....cboStores;txtPriceDate
The result is ... choose a store and date combination in the main form ... any matching records are displayed in the subform. You can navigate to the "new record" in the subform to add records for that store/date combination. But the txtNewStores_ID and txtPriceDate controls don't show the updated values until the new record is saved.
I hope this is close to what you want. It's actually fairly quick and easy to create; not so easy to describe.
You can't really do that. You are always going to have one row being worked with at a time.
What you can do is simulate it by changing the form's Default View from "Single Form" to "Continuous Form" or perhaps "Data sheet" and making is a child(sub) form of a master form.
Then you can put the store and date on the Master form, and linking to the child form using the NewStores_ID and PriceDate fields.

GridControl doesn't display the data it contains

I am using DevExpress GridControl to display information from my database through a stored procedure (ie. SELECT * FROM aTable). Unfortunately when I run the program it doesn't display any of the information, columns, etc. It displays an empty table.
I know that there is information contained by the GridView though as I have print statements that return the row count. Specifically:
Console.WriteLine(GridView2.RowCount) ' returns the number of rows that should be displayed
Console.WriteLine(myTable.Rows.Count) ' returns the number of rows that should be displayed
What should I do so that the actual data within the table (DataTable) and the DataView is displayed to the user. I know the row count is correct as when I add/remove a record the row count correlates by incrementing/decrementing.
In addition I have this same problem in C# and in Visual Basic
Ok I have the solution and it is rather easy. Go into the design view and from there go into the designer for the GridView. In there go to Columns, under Main, and add each column that you want displayed from your select statement. For each put the name that the select statement had for each column under 'FieldName' and under 'Caption' you can rename the columns to what you prefer.