not sure of what to put as the title here, but basically I am using subsonic in asp.net forms C# and I have an instance where I need to loop through a recordset and for each one call the Database to get specific information from a view about that record.
in this instance it is a venue, loop through and for each venue I show there spend and there budget, but I am at a loss as to how I can say use a gridview, execute more code on each row and then add more columns to that row.
many thanks for all advise
:-)
just as an update i have been playing with the idea of something like this:
DataSet ds = new DataSet();
ds.Tables.Add(LinqToDataTable(club.All().Where(x => x.level == 1)));
ds.Tables.Add(LinqToDataTable(ViewBudgetSpend.All().Where(x => x.periodfrom == curperiod)));
DataRelation relation = new DataRelation("budgets",ds.Tables[0].Columns["clubId"],ds.Tables[1].Columns["clubid"]);
ds.Relations.Add(relation);
still working this out though.
That is a good idea, but even better, just make one big table, not a relation. GridView works better that way, with a flat tabular data source, so you are essentially denormalizing the results into one table and binding. This works great as you can manipulate the table by adding columns, then looping through the rows and updating with your new values.
Alternatively, you can work after the fact, by using the GridView RowDataBound, and manually passing data to controls in a TemplateField column.
HTH,
Brian
Related
I'm having the following problem:
What I have is a search function that runs a query on my database using a SqlDataAdapter.
I then use:
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "table title");
gridView1.DataSource = ds;
gridView1.DataBind();
I originally got an error that I then had both a dataSource and dataSourceId, so I cleared the previously used ID. The code works, I can search the table and my query will return rows and update the table. The problem is that I want the data that it returns to be editable. I have my grid view editable, and before I run the query, I can edit the rows of the table. But after the search is run, and the gridView is filled with my DataSet, I'm unable to update the rows. If I click the edit button, it gives me an error saying the RowEditing event wasn't handled.
I looked into that event, and understand what's happening, if I were to program in the events for RowEditing/RowUpdating/RowUpdated etc. I could get it to work, but is there no better way to do this? Is there a simpler way to set my dataSource in C# and be able to maintain the editablity of the rows?
Thanks a lot!
I suppose part of the question would be would you want to? I guess you're looking for a SaveDataChanges() method which brings about a few design concerns. One is you have to remember that web pages are stateless so to an extent your web page post isn't aware of the data it's just pulled and bound to the UI. The grid view control in a round about way nicely gets the user to help you out here and click update on the row they've amended. With this you have your event handler and you can just focus on the data that's been changed and just send one UPDATE statement.
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'm very new to working with databases in C# (but not C# itself) and the whole concept of data binding, so please bear that in mind. I'm also using SQL Server CE if that affects this.
I'm trying to use a strongly-typed dataset built with VS2010 to access an SQL Server CE database, and bind WinForms controls to it to display data. I can get the controls bound fine, and they show the data that's currently in the database no problem. I can also insert new data into the database, and if I restart the application the inserted data shows up in the controls (specifically a ComboBox, but I'll be using more in the future).
The problem is that the new rows don't show up at all until I restart the app, and my understanding of data binding is that the controls should automagically update themselves. I've read about INotifyPropertyChanged but I'm not sure if it's the right thing to use here, and if it is, how do I use it for row insertions?
I'm binding the combobox like this:
DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
and inserting like this:
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
where Name is just a string pulled from a textbox.
Thanks.
EDIT: I should mention that the DB table I'm using is called tag, with two columns, tagID and tagname. tagID is a GUID, tagname is a varchar.
What you are doing with your TableAdpater.Insert is inserting your data directly into your database and you're circumventing any notifications in your application. If you do that then you have to do what iefpw said and that was to reload your datatable and rebind your controls.
An alternate method would be to add a row into your datatable. You can try something like this:
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
At this point you've added it to your DataTable and your combobox will automatically update, but the caveat is that it has not been written to your database so you'll need to make sure at some point you save to your database.
You should bind the data again and refresh the controls again like you did the first time. It doesn't just display the data. It is mechanic. It is not realtime.
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];
In a nutshell: I have a DataSet with multiple DataTables inside, which are connected via multiple DataRelations. I've created a form using databinding on this DataSet, and you could say it is pretty complex.
The scenarios is next: the main purpose of the form is the insertion of a new record. For it to be inserted, various parent-child relations must be set. It all works fine with the databinding, but the problem is next: when user wants to enter a new "child" for one-of-those parent-child relations, new form opens, he enters it, yada yada yada, - and it works. Trouble is - how should I get that new data back to the original form, to appear in parent-child combobox?
One way I know of, is to refresh the entire form; but I am looking for a solution to refresh just the DataTable containing that data. I've tried getting a new DataTable with fresh data from DB, and merging it with DataTable in DataSet, but it doesn't work: "A child row has multiple parents" exception is thrown:
// basically just a Select * using dataadapter,
// nothing programmatically added
DataTable table = tableModule.GetPlainTable();
existingDataSet.Tables["tableName"].Merge(table);
The equivalent example of what I'm trying to do is to have a Order form, with Customer combobox on it. On the right of the combobox, there is a "add" button that opens a new "Add customer" form. Just to mention (maybe it is relevant), inside the DB, "Customers" table is in relation with the "Cities" table, and that data is linked in databindning on the Orders form as well...
EDIT: one solution I can think of is to "manually" loop throughout the new DataTable, and find all rows that are not in the original DataTable inside DataSet, but I'm not sure this would work either ...
Any help / suggestion / explanation / push in the right direction would be greatly appreciated :)
Well, i kind of solved the problem with manual row copy upon insertion in second form... I have created an event with object collection (row item array), and in first form registered a handler on that event, in which I manually add the new row via:
data.Tables["tableName"].Rows.Add(e.RowItems);
after which I call AcceptChanges() on that row to make its state Unchanged, rather than Added...
This solution, although quick-and-dirty, works. The problem is, I think there is a better, more "databinding" philosophy oriented one... :=)