What is reason behind GridView in ASP.NET? - c#

I have confusion about GridView in ASP.NET.
How does the GridView exactly work?
I mean when we bind data to gridview with 100 records through GridView1.DataBind();
I have set Pageindexchanging Event and I set the Pagesize = "40" and AllowPaging="True" Then....
Now the interesting part is begins What happens when i Click on next page index of GridView is it. Once again go to the database and fetch the data.. or gridview creates its own dataset and fetch data from that dataset or anything different than this...
And one more thing is how the Sorting works in GridView?

The simple answer is yes, the GridView is simply a view placed over a DataGrid, and all the parameters specified affect how the GridView is rendered for the user. Since we're dealing with the stateless web, you will have to rebind the data.
One trick for this is that if your dataset isn't too large, you can store the DataTable in the user's session and simply retrieve it from there, saving a trip to the database. If you are dealing with a large amount of data, then you'll want to look into options for having your SQL queries function in a "paged" format so you only retrieve the rows you intend to display.
With Paging and Sorting, they serve as an event to respond to in which you resort your data and rebind to the grid for presentation.
The advantage of the GridView is that you do have a central object with a lot of functionality built in that you can use for rapid deployment. When you get a hand of how the sorting, paging, row commands, and other things work, you can do some really great things in a small amount of code.

It depends what your data source is, if your datasource supports paging, then it will fetch only the records it needs to show that page, if it does not, it will get all the records and discard the one you don't want.
For both examples, it will talk to the datasource everytime you change page.
See here for more details:
http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx

Related

Why does updating gridview also update the datasource (datatable)? If its 2 way then which is better for iteration?

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

Added column to database, bound gridview not updating C#/winforms/SQL

I've set up a simple data bound gridview that is populated via the autogenerated code for winforms. It is filling based off of the dataset I point it at.
I've updated the underlying database it's supposed to be filling off of to add an additional column to a table. However this added column is not appearing in the gridview.
I have deleted the datset and rebound it and can't find an answer online but am probably searching with incorrect terms. Is there a way to refresh the dataset in some way?
How the gridview is being filled is by:
this.xTableAdapter.Fill(this.DBDataSet.tableName);
I imagine there is a simple way to refresh the underlying dataset but cannot for the life of me find what it is.
This link had the answer. I needed to go into the dataset designer view and right click on the get, fill() query in the query area for my table. Doing so allowed me to modify the query that built the dataset, adding in the missing column.

Improve ASP.NET page_load Performance

Suppose i have many dropdownlist in my aspx page and every dropdownlist is connected to database.
So what approach that i uses for the page_load.
Any Suggestion?
I am using ASP.Net C# with SQL Server 2008.
Depending on your requirements, I can think of different ways to improve the page load performance
If values in your dropdownlists do not matter on the initial page state, you can defer binding of the controls
If there are a lot items in each dropdownlist you can make use of autocomplete or load-on-demand
Output Cache
On the data side, you can use sqlcachedependency for data that do not change that often
If dropdownlists does not depends on other dropdownlists selected value then Best way as per my suggestion is that you create One stored procedure with All select querys for binding dropdownlist and from codebehind fill it into dataset then bind datatable in dataset to appropriate dropdownlist.
Its just my suggestion because as per what i know is store procedure are faster and in this way you only need to call one store procedure.

How can I access SqlDataSource already selected data, if it is bind to a GridView?

I have a GridView and a SqlDataSource. All binding done in aspx, works correctly and Select calls an SP with parameters.
Now I would like to access the bound data programmatically, in stuctured form, not parsing back it from the UI, using cells or .FindControl or other hacks. I also do not want to reselect the data using my SqlDataSource's Select.
I've debugged the code but can not find any structured data neither in my GridView variable neither in my SqlDataSource variable. Maybe I was browsing the wrong properties or a cast was missing.
Thx in advance
I don't think you can do that. When you bind the data from an SQL DataSource to a Gridview, it fetches the data to display and then that's it. It's gone.
When a GridView updates the data, it uses the key field ID and the text in the GridView to do it. There is no other DataTable or other structure behind the scenes.

Get the number of rows which contain a certain value in one of the columns and display the number in a label

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?

Categories