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.
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 had an interview Question .
Q. how to improve performance of a Gridview contains 100000 Records of data(Datasource might be XML or DB) in c# asp.net ?
I was just blinking as i had no answer for this .Please Give me the solution for this. If possible please give one demo.
No gridview should contain 100000 records.
Your data source may contain 10 times that number of records, it doesn't matter. If you present a user more than 100 records on screen, it would be difficult to work with that data.
Select a datasource that supports paging for the gridview, that way you are only working with the number of records to be displayed on screen.
For a database you can use something like SqlDataSource.
The default XmlDataSource does not support paging, in such cases the grid will have all records in memory and handle paging itself. For xml with large number of records consider using an ObjectDataSource, where you can implement paging yourself.
If you have a choice, always select a data source that supports paging.
To fix such a GridView, I think a good approach would be to:
Add paging (as others have already pointed out)
Add filtering/searching
Add a cap/limit for maximum number of rows shown
The filtering + max limit for results is (in my opinion) a good way to keep the size of the grid manageable. If the max limit is hit using some specified set of filters (=search terms) you could show a message about it to the user and encourage him/her to specify a more specific search if necessary.
As stated before, a gridview should not hold so many rows.
In your interview, they wanted to see if you can say that its bad implemented rather then to check your programming skill.
But if one insist for good performance, I would suggest to use Repeater instead of Gridview.
Cheers,
Ran
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.
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.