Multiple row input in asp.net for editing database table - c#

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/

Related

How to implement custom paging in gridview in ASP.NET using C# with data modified in code-behind

I have looked-up custom paging with gridviews in ASP.NET, but it seemed to work only for displaying exact same rows from the database to the web page.
In my case, I currently use default gridview paging to select all rows from the database, then process that data in the code behind of my aspx page, and then show custom rows based on that processed data in my gridview.
My question is: I want to know how to select a certain amount of rows from the database that will reflect just enough "custom" rows for 1 page in my gridview on the web page.
Note: I process the data on the front-end and group them in custom rows based on a common, same FKID value.
Would really appreciate the help,
Regards.
Does each FKID-value have the same amount of rows in your table? I assume this isn't the case.
I think your options are the following:
You could leave your logic as-is. If the performance isn't too bad, I think this is the easiest solution.
Create a view at database-level. This view should then directly provide records as you need them as a row in your GridView. This puts the paging-logic at the database-level, and is performance-wise the fastest option I think.
Do the same as option 2, but then at your application level. That could work this way: Construct a select query that gives you the count of distinct FKID values, and a query that can give you a list of FKID values based on a 'startindex' and 'number of rows'. For each FKID returned, fire another query fetching the corresponding rows. Apply your logic, and return the page of rows to your GridView.

filterable and sortable datatable/datasource

I have a huge table and I need to implement a (web) control to allow me to browse through all records, paginate, filter and sort them. My first thought is to implement a DataGrid/something and to put as DataSource a DataTable which is read (with a "view" or simple sql "select all" statement) and then to filter the DataTable object (DataView.RowFilter, etc). As far as I am aware, this sorting/filtering is done on the client-side, after all records are read. Is there any more elegant/efficient way to do this (meaning - just read the records you need from the database server)?
For controls like GridView the default paging retrieves all rows from the database each time a page is selected.
Not exactly efficient is it? However, you can use custom paging to retrieve only those records you need for the currently selected page.
Here is an example: http://www.asp.net/web-forms/tutorials/data-access/paging-and-sorting/efficiently-paging-through-large-amounts-of-data-cs

Adding rows programatically to a DataGridView that is data bound?

I have a problem and I can't seem to be able to solve it. I thought someone here will be able to help out.
I have a form that has a DataGridView of customers. Now, I want to add several customers to this DataGridView without actually adding them to the database. This is because the client must be able to create a list of clients and when thats done add them all at once.
I have tried this:
string[] array = {"Microsoft", "Redmond", "Something"}
dataGridView.Rows.Add (array);
Now this can't be done because I get an exception saying something in the lines of you cant add rows programatically to a DataGridView that is data bound.
Now I have also read that this can be solved by using a table adapter to insert the rows instead of directly adding them via. a DGV. But this is also not possible because I use custom headers in the DGV because existing data is fetched via JOIN's so if I add them via a TableAdapter I get an exception that the it doesn't match the databases table schema.
Now I am really lost... Anyone know of a (halfway) elegant solution to this problem?
Thank you
Bind your DGV to a BindingList<YourObject>, where YourObject can be a simple class with properties reflecting your database schema. Initially populate the BindingList from the database, and the DGV will automatically add YourObject instances to the list. When you're ready to commit the changes, do so manually from the BindingList.
In order to show those rows, just do a UNION (-like) action before the databinding. Ie get your list of records, add the special ones and then databind.
Updating will require some special action(s) too. But with a total lack of details about your datastructures it's impossible to say what.

C# datatable, dictionary, datagridview

I have a project where I want to:
1) grab data from a sql server database,
2) pull the data into a c# program,
3) keep the data persistent - (store in a dictionary?)
4)view the data using a datagridview,
5) update the data, which would update the datagridview, the dictionary, and the database
What would be the most efficient way to implement this project?
My current thinking is to use a datatable to keep data persistent in program, and to allow data to be easily viewable. As well.
Any thoughts?
You can bind a DataGridView directly to a datasource (SQL Server) as described here
Create DBML, and use LinQ.
Get your data, and bind them to custom DataTable (which is created with your own column names, types etc.)
Bind your DataTable to gridView.
Put a Update button, and when user selects a row, clicks an update button, get selected row, Update this row with LinQ and refresh your gridView.
phsr's answer takes care of the UI and database. In order to store it locally you could use a SQL Express database, or an easy alternatively would be to simply store it in local XML files, see here for some help with that:
http://msdn.microsoft.com/en-us/library/fx29c3yd.aspx
Hope that helps!

Cleanest way to implement collapsable entries in a table generated via asp:Repeater?

Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of a very large system, so it really wouldn't be wise for me to revise the table structure (the app is filled with similar tables).
This is a webapp in C# .NET - data comes in from a webservice and is displayed onscreen in a table. The table's rows are generated with asp:Repeaters, so that the rows alternate colers nicely. The table previously held one item of data per row. Now, essentially, the table has sub-headers... The first row is the date, the second row shows a line of data, and all the next rows are data rows until data of a new date comes in, in which case there will be another sub-header row.
At first I thought I could cheat a little and do this pretty easily to keep the current repeater structure- I just need to feed some cells the empty string so that no data appears in them. Now, however, we're considering one of those +/- collapsers next to each date, so that they can collapse all the data. My mind immediately went to hiding rows when a button is pressed... but I don't know how to hide rows from the code behind unless the row has a unique id, and I'm not sure if you can do that with repeaters.
I hope I've expressed the problem well. I'm sure I'll find a way TBH but I just saw this site on slashdot and thought I'd give it a whirl :)
When you build the row in the databinding event, you can add in a unique identifier using say the id of the data field or something else that you use to make it unique.
Then you could use a client side method to expand collapse if you want to fill it with data in the beginning, toggling the style.display setting in Javascript for the table row element.
just wrap the contents of the item template in an asp:Panel, then you have you have a unique id. Then throw in some jquery for some spice ;)
edit: just noticed that you are using a table. put the id on the row. then toggle it.

Categories

Resources