What we're doing - we have some DataGrids bound to some DataViews and when the user hits a 'submit' button we want to pass all the changed rows to a SPROC. There are some complexities here I can't go into detail about but...
what we need - a list of the modified rows ordered by the time they were modified. Is this possible? I doubt it is possible with the current DataView class so I was wondering what other options I have?
Using .NET 4.0
Kind regards,
Fugu
One way could be to manually add a column "ModifiedTime" that will hold the modified date and time. Every time a row is modified, modify the "ModifiedTime" column by updating its value to current date and time. Now you can simply sort by "ModifiedTime" to get required results.
Related
I have a datatable in code. And the datagridview in the Ui.
In code I have done gridviewName.DataSource = dtTable1
Now in the UI I can see gridview populated with the table data. In the UI gridview I can update the cell values and/or delete the data rows. Upon doing any changes to the gridview, the changes automatically flow back into the data table.
I am bit confused at this point because I had thought it is a 1 way connection from data table into datagridview. Is this 2 way by design? If yes, then subsequently if I want to perform an operation per row, like send an email per row, then it is recommended to iterate over the gridview or the data table?
Yes, the 2 way data flow is by design; it makes creating applications a lot easier. If you don't want your user to edit a grid you make the grid read only, but typically you show data to a user, you let them edit it and save it. That would get a lot more hard work if you made data binding a one way thing the grid is connected to the datatable directly; it doesn't copy the data it finds into its own internal data array
Always loop over the datatable, read it and edit it directly; the grid will update accordingly; always avoid looping over the datagridview. As the embodiment of model-View-controller your code should manipulate the model (datatable) not try and manipulate the model via the view/controller that is intended for the user (the datagridview)
If you add a DataSet type file to your project and design a strongly typed datatable inside it your life gets easier. Your code looks like:
foreach(var r in soneDataset.EmailQueueDataTable){
mailer.Send(r.From, r.To, r.Subject, r.Message, r.RetryAttempts);
}
With a standard datatable everything is done with string column names or worse, ordinal positions and needs casting:
foreach(DataRow r in EmailQueueDataTable.Rows){
mailer.Send((string)r["From"], (string)r["To"], (string)r["Sujbect"], (string)r["Message"], (int)r["RetryAttempts"]);
}
Intellisense won't help you with the column names either; you'll only find out at runtime that I made a typo in Subject
I have a gridview and sqldatasource.
I want to display in a label, the number of rows from the gridview which contains a certain value in one of the columns (in the form_load event)
I thought about looping through all columns of the gridview but it will take a lot of time for this and maybe there's another way of doing this.
Can someone help me finding the "other way"?
Thanks,
Best solution would be to let the database handle filtering - you'll get much better performance that way than looping over the data on application server.
Perhaps create another SqlDataSource with an SQL statement containing appropriate WHERE condition and bind it to your label?
I have used a ultragrid in my windows application to show records of a table of my database by set its DataSource property to the datatable that returns from that table.
the number of rows can be about 100000 rows.
Now i have 2 questions:
1) I want to let user to select several rows of this grid and then edit them all.to do this i need to know which cells of these selected rows have same values and which of them have not and then show the same columns in a form.
the first solution i thought about, was check all columns of all selected rows through 2 foreach loop.Is there any better solution for this??
2)finally when the user press save button, the updates should apply on the table.what is the best way to do this??? (Use IN command or UpdateCommand of sqldataadapter or ...)
According to my analysis,
1) you can do the grouping in the ultragrid based on which all columns you want to see and save the data. After grouping the similar columns would appear together and it will save a lot of time in data analysis and figuring out which column data are same in different rows. Once you have got the list of similar data you vcan handle them easily through for each loop or even for loop as you would be able to access the count of rows thus generated.
2) You should look at using the System.Data.SqlClient.SqlBulkCopy for this. Here's the documentation, and of course there are plenty of tutorials online.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx
One should avoid IN when no. of records are more as it affects the performance. If you dont want to use sqlbulkcopy, then you can also go for UpdateData() of ultragrid(very useful feature of ultragrid)
You can use the UltraGrid provided method call UpdateData() to update all the multiple rows with out need to track changes row by row or by cell
The UpdateData method updates any modified information in the grid, sending it to the data provider. When the update is complete, any rows that were marked as having modified data will have that mark cleared.
The UpdateMethod needs to exit from cells that are still in edit mode, this is a bug and to resolve it you will need to use it as shown below
ultraGrid1.PerformAction(Infragistics.Win.UltraWinGrid.UltraGridAction.ExitEditMode);
ultraGrid1.UpdateData();
More details on why to use it this way can be found here http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.Aspx?ArticleID=4220
To answer you 2nd question you will have to use the UpdateData method this way
e.g.
private void ultraButton1_Click(object sender, EventArgs e)
{
ultraGrid1.UpdateData();
this.dbRowsTableAdapter.Update(this.testDataSet);
ultraButton1.Enabled = false;
}
Again it seems UltraGrid keeps its local data pure to itself hence you will have to call the Update method of the TableAdapter, which means you will have to create a DataSet and fill it with the SqlDataAdapter.
Please see forum explaining clearly why UltraGrid.UpdateData method does not commit back to database here http://www.infragistics.com/community/forums/t/57161.aspx
I could not find any solution for my first question so i have used loop to compare all selected rows of my grid and diagnose the common values.
but i have used SqlBulkCopy for my second question.
Can following thing be done?
The red rectangled datatable should be modified as the other row.
I am using c#, DataTable with MS-SQL.
I want to give shown type of view of second row to the user in a windows.
I'll be having at least 500-600 rows like this out of 1000 rows. Which can be shorten down to 1000-600/3 = 800.
(Perhabs, I can take this chance because the operation will not be time consuming.)
The user will be having ease in putting data only once in place of puttinig it thrice .. will save time for them and over all performance.
Please ask question for help me solve this problem.
Thanks in advance.
There is an article about this here: https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/, "Concatenating row values in Transact-SQL".
The conclusion is that this is not a trivial task and can be achieved using SQL or in code.
I would probably perform the database querying in code and then modify the DataTable and databind the grid with the modified DataTable. If the grid allows editing, then you have to perform the reverse transform before saving it to the database.
If I get my dataset into a DataTable then I can do with it as I like, but I have the complexity of handling sort, paging and caching manually. I'm trying to avoid this for now.
If I instead use a SQLdataSource that's all free.
I need a grand total row, the contents of which I display outside the gridview.
I know that I can get that by hooking the RowBound event in IIS and summing up every row I see, but that seems a but complicated and pedestrian.
I know that I can manually do a SQLdataSource.Select on my SQLdataSource (it's cached) to extract a DataTable from that, and then use DataTable.Compute to sum the columns, but this seems a bit hack and I'm not sure how efficient two "selects" really is even with caching.
My preferred approach would be to get SQLserver to do the grunt work, using Group By Rollup, which gives me the last row of the result set with the totals I want in it. The problem then is that I have the totals row in my GridView, which I don't want in there as I need to put the totals somewhere else (no point in having them on the last page where they can be sorted and paged). I guess I could again catch row bound events and make this totals row invisible, but that's a bit hack and may confuse the paging.
So I'm wondering if there's a neat way to do this?
When you mean large data set, I am assuming a million plus?
I would suggest caching the result of the grand total. There's no need to recalculate that every time you view/page the table. Create observable events when you create/update/delete the table to refresh the grand total info? (Because I do not see you mentioning filtering, which is the only time when the grand total changes without the dataset changing)
Or maybe have the grand total info on a timed expire cache. If the data is suffeciently large and volatile, sometimes you would have to consider the business value of having an exact uptodate grand total. Usually there's no business value in that. A somewhat close enough value from a few sec ago is fine.
As for SqlDataSource control, personally I hate it. Whatever happened to the Presentation/Logic/Data tiers?!...