Hopefully simple, but can't find any such option.
I have a data table -- has say... 10 rows in it. Some fields on the form are bound to the table.columns respectively by name.
On another form that HAS a grid, as I scroll the grid, the detail fields are refreshed as expected since the grid does some magic to trigger the DataTable record changing event.
WITHOUT using a Data Grid, How can I direct the table to go to a specific row for load/display refresh on the form... ex:
DataTable MyTable = new DataTable();
MyTable = GetResultsFromSQL(); // returns the 10 rows
MyTable.LoadTheDataForRow(3);
MyTable.LoadTheDataForRow(7);
MyTable.LoadTheDataForRow(2);
I know I can use a foreach row in the table, but need explicit use as I don't want to go through all rows, but need specificity to specific ones.
I've looked at the LoadDataRow(), but that appears to be for pushing data back to a server. NOT what I want... I just want to have the "Current" row of the table to be of a specific one...
Thanks
After further research, I've found that a FORM based control "BindingSource" (or derivative) allows this, such as a grid. But, obviously, there's something the .Net engine is doing under the hood to ultimately "Load" a given row into something that ultimately triggers back to the "BindingSource"... The DataTable has RowChanging and RowChanged events which appear to be triggered by OnRowChanging / OnRowChanged delegates, but how can we tell the data table which "row" we want it as the active one.
The form controls can do this for their binding sources, but what is really happening under the hood to trigger these OnRowChanging events... I don't want to re-load a data table, rows, etc, just change what is considered the "Active" row, as in a grid, listbox, combobox, etc.
Have you tried:
MyDataTable.Rows[3];
MyDataTable.Rows[7];
MyDataTable.Rows[2];
Related
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/
I have a DataGridView that displays data from an SQL table. It is using sqldependency so that an alert is made when the data changes, and the DataGridView immediately displays the updated data. New rows will be added to this table regularly, and when this happens I want to take only those new rows from the first data grid and populate a separate DataGridView.
I've experimented a bit with RowAdded event, but it hasn't been entirely straightforward because it creates an alert for every row in the table whenever the grid view is updated by sqldependency.
Does anyone have experience doing something similar?
Let's call the first DataGridView with all the rows Grid A and the DataGridView that shows only new rows Grid B. Don't define the contents of Grid B by observing changes to Grid A. Instead, create your own definition of what a "new row" is. One easy way to do this is to make a copy of the contents of Grid A during every update (in the OnChange event of our SqlDependency object). Then, on the next update you'll be able to select rows into the "new rows" DataSet.
Here's the pseudo-code for the OnChange handler for your SqlDependency object (this should be watching the SqlCommand that fills Grid A):
Run a SqlCommand that selects rows from the Grid A datasource with an NOT IN clause that excludes rows with ID in a 2nd table called LastUpdate (see step 2). This will be the data source for Grid B
Run a SqlCommand that copies all rows of the Grid A data source to the LastUpdate table. It may be easiest to just delete the contents of LastUpdate first, then run a statement of the form: INSERT INTO LastUpdate(ID) SELECT ID FROM table_x
Update the Grid A normally
The point I want to emphasize is that this update strategy creates an effective separation between data queries and the UI. In other words, the data that drives Grid B does not in any way depend on Grid A. If you stick with this principle, you should wind up with a much more maintainable application.
I have a situation where I have a grid loaded with data. It is not data
bound. Clicking an image opens up another form that allows the change of
the data in the grid.
Right now the changed data (1 row) is written to the DB and the whole
grid is reloaded from the DB which now incorporates the change.
My question is can I update the data in a data view? That way I can
right the change to the DB and update the DataView without having to dip
the DB every time and essentially avoid reloading a 1000 rows of data
because I changed one.
How do I change one row in a DataView?
This is not a generally supported feature, because it would only work if you were doing a straight select * from table query, with a known PK. Any time you had any joins, or aggregate functions involved etc, the entire query would need to be executed in order to get the value of that row.
Why aren't you using databinding? If your grid is bound to a datatable, and you do your update that way, then the grid would be automatically updated by virtue of being bound to the table.
Even if you do not want to do full round-trip databinding, if you still use the table, just update the table with the appropriate changes, and then re-bind the grid.
I have a DataGridView bound to a LINQ to SQL query expression. I want it to be editable for updates, but for inserts I want users to use separate controls below the grid (textboxes, date pickers, etc - currently not bound to anything). I have an "Add" button that creates an object of the linq to sql entity class based on the values in the controls, and then calls InsertOnSubmit(). When I later call SubmitChanges(), any updates from the grid, and any objects added are all correctly persisted to the database.
My problem is that any new objects are not added to the grid, before or after the call to SubmitChanges(). I would like new objects to be added to the grid as soon as they are created. Also, I only want the controls below the grid to be used for inserting new records, not editing existing records, so I don't think they should be bound to the data source...What is the best way to make this happen?
I did try simply re-setting the DataSource for the grid (ie dataGridView.DataSource = db.<TableName>, which works, but is clumsy because it scrolls to the top of the grid again - I'm sure a better method exists.
(Pls excuse the n00b question, I'm very new to all this .net stuff :P)
The first thing to try is GetNewBindingList(), but this thread: "Linq-SQL canonical editable datagridview sample code" has some thoughts for other scenarios.
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.