Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView sort by a column whose header is dynamically created

Reply
Thread Tools

GridView sort by a column whose header is dynamically created

 
 
gnewsgroup
Guest
Posts: n/a
 
      04-11-2008
I have a GridView, in which the header of one column changes depending
on the selected value of a DropDownList outside of this GridView.

I did this dynamic header through

protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[5].Text =
myDropDownList.SelectedValue;
}
}

In the declaration of this GridView, I have

AllowSorting="true"

and of course I have a Sorting event handler.

Every column is sortable except this column with the dynamic header.
If assign a static value to this header and remove this RowCreated
event handler, this column becomes sortable, too.

So, the question is: How can I make the column with a dynamic header
sortable?
 
Reply With Quote
 
 
 
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      04-11-2008
It is essentially the same as sorting with a non-dynamically created header,
except you have to control the sort. For a simple sort, you can use a
DataView to bind and set up the sort there. With this form of sort, you can
cache the data, most likely in ViewState, although there are applications
where this is not acceptable.

If you also need paging, you will have to write custom software to do this
and will probably have to retrieve the data each time there is a sort so you
can get a pointer to figure out start and end of the page.

If you want to see how Microsoft does it, you can use Reflector on the .NET
assemblies or, in VS 2008, step into the code. Both can give you a clue of
how MS accomplishes sorting.

Here's a couple of articles:
http://lakshmik.blogspot.com/2006/06...nd-custom.html
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=428

I am kind of fond of the idea in the second one, although I have not tried
it. Binding with generics would take a bit of work to massage the data
intitially, but the benefits outweigh the extra work.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://gregorybeamer.spaces.live.com/lists/feed.rss

or just read it:
http://gregorybeamer.spaces.live.com/

*************************************************
| Think outside the box!
|
*************************************************
"gnewsgroup" <> wrote in message
news:67c552e2-ed82-4b90-a187-...
>I have a GridView, in which the header of one column changes depending
> on the selected value of a DropDownList outside of this GridView.
>
> I did this dynamic header through
>
> protected void GridView1_RowCreated(object sender,
> GridViewRowEventArgs e)
> {
> if (e.Row.RowType == DataControlRowType.Header)
> {
> e.Row.Cells[5].Text =
> myDropDownList.SelectedValue;
> }
> }
>
> In the declaration of this GridView, I have
>
> AllowSorting="true"
>
> and of course I have a Sorting event handler.
>
> Every column is sortable except this column with the dynamic header.
> If assign a static value to this header and remove this RowCreated
> event handler, this column becomes sortable, too.
>
> So, the question is: How can I make the column with a dynamic header
> sortable?



 
Reply With Quote
 
 
 
 
gnewsgroup
Guest
Posts: n/a
 
      04-18-2008
On Apr 11, 9:55 am, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamM> wrote:
> It is essentially the same as sorting with a non-dynamically createdheader,
> except you have to control the sort. For a simple sort, you can use a
> DataView to bind and set up the sort there. With this form of sort, you can
> cache the data, most likely in ViewState, although there are applications
> where this is not acceptable.
>
> If you also need paging, you will have to write custom software to do this
> and will probably have to retrieve the data each time there is a sort so you
> can get a pointer to figure out start and end of the page.
>
> If you want to see how Microsoft does it, you can use Reflector on the .NET
> assemblies or, in VS 2008, step into the code. Both can give you a clue of
> how MS accomplishes sorting.
>
> Here's a couple of articles:http://lakshmik.blogspot.com/2006/06...x?quickdoc=428
>
> I am kind of fond of the idea in the second one, although I have not tried
> it. Binding with generics would take a bit of work to massage the data
> intitially, but the benefits outweigh the extra work.
>
> --
> Gregory A. Beamer
> MVP, MCP: +I, SE, SD, DBA
>
> Subscribe to my bloghttp://gregorybeamer.spaces.live.com/lists/feed.rss
>
> or just read it:http://gregorybeamer.spaces.live.com/
>
> *************************************************
> | Think outside the box!
> |
> *************************************************"


Thank you very much for the references. The problem for me right now
is that the dynamically created header isn't even clickable, unlike
other static header, which you can click and sort.
 
Reply With Quote
 
AlphaQuam AlphaQuam is offline
Junior Member
Join Date: May 2008
Posts: 1
 
      05-20-2008
I ran into this same problem. Originally, I was using the Row.Cells collection as well. I would suggest trying the following...
Code:
GridView1.Columns[5].HeaderText = myDropDownList.SelectedValue;
Be careful of when you make this assignment. I'm not sure what you're binding your GridView to, but changing the header text when using a LinqDataSource after its OnSelecting event (such as in the GridView's OnDataBinding event) will cause that event to fire again after the headers have been updated. Obviously, this could cause some serious performance issues, as well as other potential problems.
 
Reply With Quote
 
bigal bigal is offline
Senior Member
bigal's Avatar
Join Date: Jul 2005
Location: Northern Virginia, USA
Posts: 1,487
 
      05-20-2008
It appears somebody fixed your problem AlphaQuam - of not being able to post. Please enable your private messages so people may contact you and welcome to the Velocity Reviews forums.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pump data by row and column to dynamic created column in gridview johnsonlim026 ASP .Net 0 02-27-2010 05:29 PM
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Image in header column (not replacing column header text) hansiman ASP .Net Datagrid Control 3 02-07-2004 12:17 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57