ASP Dot Net ListView DataBindingASP Dot Net ListView DataBinding

GridView inside a ListView 
ListView is a very flexible control for rendering custom formatted outputs. I have already managed it to get a horizontal menu with liquid layout. Its fantastic set of templates allows most of the rendering to be easily done. With a little bit of CSS knowledge we can avoid rendering tables for most of the lists. It is not only flexible in rendering, but also very good in handling controls inside its template. To explain this i have placed a GridView inside ItemTemplate to load in all cells.

Asp.net ListView ItemDataBound to load GridView 
For explaining the usage of listview ItemDataBound, I have chosen a GridView to be placed in a template (fetched using findcontrol). So based on the ListView's values the GridView will be populated. As usual, I have taken Northwind as a sample db. Further Category and products tables are selected for ListView and GridView respectively. Initially categories will be loaded into the ListView, and then Products will be loaded into the GridView contained in the respective cell of the ListView. Refer the following picture for the expected output.

Bind both ListView and GridView 
I am not going to explain how to read the data into a dataset here, as it is already explained in several articles (refer asp.net category for that). For loading ListView, it is very easy as it can be directly bound to Categories table. But for the GridView, we have to wire up listview ItemDataBound event to access it. It can be accessed using the event arguments in the event listview ItemDataBound. E.Item.FindCountrol ("GridView1") will give the Gridview1.

Access Data GridView from ListViewItem 
Before fetching database values for the respective category, we have to get the GridView from the ListView. For this first the DataKeyNames has to be declared in the ListView (lets take Category ID for this sample). With this later we can access those values in the listview ItemDataBound.

DataItem DataItemIndex is not visible directly 
One important item to be noted here is, unlike GridView you can not access the current row index in the ItemDataBound event directly, and you have to typecast the e.item to ListViewDataItem and then the DataItemIndex to fetch the current index.

Bind Data to GridView 
To fetch the products under the specific category, we need to pass the Category ID. This can be taken from DataKeyNames as discussed earlier. Use the DataItemIndex to fetch the category ID from the DataKeyNames for the current row. After fetched the values to the DataTable just bind it to the GridView using DataSource and DataBind.

by Pon Saravanan
References and Bibliography
Pon Saravanan
For the article source code Asp.net ListView and also for visual basic reference VB.Net Tutorial.
Rated:NR/0 Votes
9 Views
Add To My Article Reading List
Add To My Article Reading List
Print Article
Print
More Article By Pon Saravanan
More Article by Pon Saravanan
Share
More Articles From ASP.NET
More Articles From ASP.NET
Related Articles and Readings
How to Secure Your Data in the Office Network By: Simona Rusnakova
The data of any company is essentially the infrastructure on which all the organizational activities rest Information is the key to any operation in the commercial world and this makes it an extremely valuable ...
Data Recovery Methods for Three Common Situations By: Nitesh Ahir
Most people around the world are regularly working with computers these days. It is common to every one of us that we lost our important data, files, photos, videos, emails etc. on hard drive, external hard drive, usb flash drive, digital cameras, sd cards, portable devices and more accidently. ...
Secure Your Office by Protecting Sensitive Data By: Simona Rusnakova
If a person is managing business and its financial requisites online, it becomes very important to secure the sensitive data of the business The main danger lies in the situation where the main server in the office stores the complete backup files ...
Data Relocation and How to Make Your Data Work For You By: Anna Notton
The digital age has arrived Sometimes it seems like everything is already computerized, and then a new way to computerize something makes the ...
Data Storage Over the Years By: Adriana Notton
Data storage devices have changed a lot in the last several years since their inception With the advent of increasing requirements, the need to create larger capacity in smaller packages has been ...
The information provided in this article and/or the comments is the sole responsibility of their respective authors and does not necessarily reflect the opinion of ezinepost.com. ezinepost.com  does not endorse any article and/or comments published by our web users unless otherwise noted. 

Member Panel

login to submit articles and more

StatisticsEZINEPOST.COM

  • » Active Categories: 419
  • » Active Articles:252609
  • » Active Authors:31915
  • » Active Members: 38222
  • » Statistics Updated:
    - Thu May 7th, 2020 12:50AM EST