473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reusing code for multiple GridViews on same page

I would like to reuse the code for gridview sorting for each of the several GridViews that I have on one page. I've not done this before, so I am seeking a more elegant and reusable solution than coding events for each of the 4 gridviews I have.
Feb 4 '09 #1
8 7435
Frinavale
9,735 Expert Mod 8TB
Have you implemented a method that sorts which you can call when ever a GridView needs to be sorted?
Feb 4 '09 #2
I am doing "custom" sorting so yes I have. The GridviewSortEventArgs do not specify the Gridview itself, so I'm curious how to tell the event handler which GridView to sort.
Feb 23 '09 #3
Frinavale
9,735 Expert Mod 8TB
Ok, you've created a method that does sorting.

One of the parameters that your method requires should be a reference to the GridView that called the method.

For example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub MyGlobalSortingMethod(ByVal theGridView As GridView, ByVal theSortParameters As GridviewSortEventArgs)
  2.  
  3.  'Sorting code
  4.  
  5. End Sub
Please note that I used ByVal in the above code.
The reason for this is because a GridView is an Object, which means that it's memory location is passed as the value....

If you're not using VB.NET, just ignore this.
Feb 23 '09 #4
OK, that helps, except that I don't know how to get the GridView control's name from the postback. What I am trying to do is have OnSorting point to the same method for 4 out of 5 gridviews on one page.
Feb 23 '09 #5
Frinavale
9,735 Expert Mod 8TB
The "sender" parameter should be the GridView.
Feb 23 '09 #6
Insert embarrassed smile here. Thanks for your help.
Feb 23 '09 #7
Frinavale
9,735 Expert Mod 8TB
No problem :)
Don't feel bad, I used to make the same mistakes when I first started using .NET.
It just takes some getting used to :)
Feb 23 '09 #8
Hi
I have already used this code. firstly , i had to face same problem. but
now is working properly. I think, your problem is same. You can make a common function and call on sorting. In this function you have to pass argument according to each gridview.

Expand|Select|Wrap|Line Numbers
  1.  Protected Sub gv1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv1.Sorting
  2.  
  3.         Dim colum As String = e.SortExpression
  4.         sorting(colum)
  5.         binddata1()
  6.  
  7.  
  8.  
  9.     End Sub
  10.  
  11.  
  12.     Function sorting(ByVal colum As String)
  13.         hiddenSortColumnLeads.Value = colum
  14.         If hiddenSortOrderLeads.Value = "asc" Then
  15.             hiddenSortOrderLeads.Value = "desc"
  16.         Else
  17.             hiddenSortOrderLeads.Value = "asc"
  18.         End If
  19.     End Function

Implement this code.
Feb 25 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rob Meade | last post by:
Hi all, I'm in the middle of finishing a page and I've noticed that I have a chunk of code that is used in two places - its exactly the same - I guess I can rip this out - save it seperately and...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
2
by: Hennie | last post by:
I have 2 gridviews on a page, each with own SQL Datasource pointing to different tables on same database. In the 1st date format is "dd/MM/yyyy" Other 1 "MM/dd/yyyy hh:mm:ss" In the SQL...
0
by: HP | last post by:
Hi there I have a datalist control with some bound controls in its Item Template and a gridview bound to one of those fields (residing also in Item Template). I've found out that when I click...
3
by: AishaKhalfan | last post by:
Hi, :wave: I have many search criteria in my program, such as: search by name, search by age, search by gender, search by city the user can search by name only, or by name and age, or by...
2
by: ShaijuThomas | last post by:
Hi guys, terribly in need of help. There are multiple gridviews on my web page. I would like the data to be downloaded in a single excel book but with multiple tabs. I don't want client-side scripts....
0
by: gvera | last post by:
Hello, I am having a hard time trying to get the checked values of multiple Gridviews and bind them into one Datalist.
2
by: DK | last post by:
I have an asp.net web site which has GridViews on many pages. They are different except they all have a common Pager Template. On the DataBound event, I customize this template based on the number...
1
by: dan | last post by:
Hi, I think I need a gridview, formview, another gridview, and another formview on a single page. There is a parent-child relationship between the first gridview and the formview and then...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.