I have a GridView that is bound to a select statement from a table. That table contains a lot of keys out to other tables that are just IDs. I would like the GridView to "dereference", as it were, the id of the field in question and display the human-readable name found in the other table.
At the moment, the options that come to my mind are composing a DataSet by hand in the codebehind and binding the GridView to that instead of the SqlDataSource or creating a stored procedure to return the table already "dereferenced". Any other ideas or recommendations for this situation? I am using .NET 2.0 per employer mandate.
I am not entirely sure what you mean by "dereferencing" but if you just want to display the value from the tables refered to by foreign keys then just bind your gridview to a query that joins all the required tables in a flat dataset. e.g.:
SELECT Table1.Field1, Table1.Field2, Table2.Field1, Table2.Field2
FROM Table1
LEFT OUTER JOIN Table2 on Table1.Table2ID=Table2.ID
This can be done either in a stored procedure or by directly specifying this as the SelectCommand property.
I just ended up creating a view that automatically dereferenced the ids to the other tables and using that for the basis of the GridView.
Related
I'm sorry for not being able to explain this but now I'll elaborate what I want to do. I'm working on a .NET software(Windows Form, C#) with SQL Server, I'm using devexpress xtrareport(First time actually) for report printing and here comes my porblem, I have a table (let's call it invoice table) and it contains a column for products which contains table name for the products table of that invoice, each time the product table is new and I don't know how can I print that product table? How can I even add it as a datasource when It's not created yet.
you should not use the stock SqlDataSource, instead you should use the ObjectDataSource so you will manually fill the DTO and pass it to report
I have a MSSQL DB, with lot of records. I have a column in my table with serial number (Auto generated). I am populating the DB in grid-view (C#), I want to sort the gridview based on this serial number column while the gridview loads.
Well why don't you pass already sorted data to gridview.just use order by clause in your sql query and pass the query results to gridview which will be already sorted.
select * from tablename order by SerialNumber(Sql syntax)
About the second solution u can use dataview class to sort data before passing to gridview
I have 3 Tables as follows:
Main Table = (ID, name)
Parameters Table = (ParameterID, Parameter Value, MainID)
Parameters Names Table = (ParameterID, ParameterName)
Main Table --> Details Parameters Table is a one to many relationship
Parameters Table to Parameters Names is a one to one relationship
I want to databind and display ALL parameters Name in column one of the datagridview and parameter values in column 2 for a particular main ID.
My current solution is :
LINQ query that does a left outer join from parameter names table to parameters table which gives me the Datagrid view (all parameter names, parameter values).
Now the issue is, this is not really bound to Entity Framework as it is just a projection, I know I can loop through the datagrid entries and achieve what i want,
Is there a better approach to solve this i.e. can we do a join and also bind to controls directly in a windows form datagrid? This is so the user form can directly allow a user to update/delete/insert items from a joined result.
I'm populating a DataSet with a SQL Table View, the view provides data from 2 SQL commands connected via UNION.
Select ID,A,B,C,[...],'Appointment' from Appointments
Union
Select ID,A,B,C,[...],'Task' from Tasks
I'm displaying the data in a grid view and the end user can change the data.
Via ID and the fields 'Appointment' / 'Task', i'm able to identify the edited record.
But how can i Update the according record in the according tables?
From time to time, I need to create an input control which allows multiple rows of input, the type of input and number of columns could be different in each case but typically it would be represent a database table being edited, so it should also be possible to store an id value within each row. It should also be possible for the user to add new rows to the input if they have more data to input.
I have tried a couple of approaches to this but they are all very long winded for what seems like such an obvious and common scenario. I'm thinking that there must be a simple way to do this that I have missed.
The way that I usually solve this is by using a list view, enter all of the input controls required within the item template and use a html table for formatting, with each item being a row of the table. If there is existing data to be edited, I would load the data from the database, add a blank object to the results and bind it to the list view. If there is no existing data, I would create a collection with a blank record in it and bind it to the list view. I add a button for adding a new row. When clicked, this button retrieves all of the existing data from the list view by iterating all of the listview items and populating the data into a collection, which I would then add a blank object to and rebind the listview with the results. When saving, I would retrieve the results by iterating the listview items again and using a hidden field within each row to store the record id. This would determine whether a new record was added or an existing record was edited.
This approach works for me but I'm sure there must be simpler ways to achieve this. Looking forward to seeing how other people solve this.
For web forms, use a GridView. You can set its properties to allow editing, deleting, new rows, sorting, and paging.
Check out the MSDN example here that shows how to use it. If you bind a datasource to it, it will figure out the columns and adjust dynamically then, or you can predefine the columns you want for more customability.
You are talking about bulk insert/update. You could use XML insertion/updation for this purpose. Get all the data to a DataSet ds variable. Then use ds.GetXml() method to convert the dataset to XML string. Pass this to an XML parameter into SQL server which has the datatype 'XML'
INSERT INTO YOURTABLE
SELECT
V.VOI.value('(.)[1]', 'int')
FROM
#Input.nodes('/DATASETNAME/DATATABLENAME/') V(VOI)
Use this link to learn more
You need dynamic table with Add, View,Edit and delete operations on each data.
I would suggest using DataTable Jquery component. there are tons of examples on Data operations, and you can plug this with any Server technology including ASP.net
http://editor.datatables.net/