Up Down GridView Icon Image for Column Sortinghttp://forums.asp.net/t/1032944.aspx/1?Up+Down+GridView+Icon+Image+for+Column+SortingMon, 01 Jun 2009 10:03:08 -040010329441421067http://forums.asp.net/p/1032944/1421067.aspx/1?Up+Down+GridView+Icon+Image+for+Column+SortingUp Down GridView Icon Image for Column Sorting <p>I'm trying to add an Ascending and&nbsp;Descending sort direction&nbsp;Icon Image&nbsp;next to&nbsp;the header&nbsp;text of my&nbsp;ASP.NET 2.0 Gridview Columns.<br> I've searched and found some examples that seem to do this but have had difficult time recoding them from C# to VB or some just seemed overly complex for what I want to do.</p> <p>Does anyone have a simple VB solution for sort icons?</p> <p>Thanks,<br> Ethan</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> 2006-10-06T21:31:51-04:001422388http://forums.asp.net/p/1032944/1422388.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>Hi,<strong>ethanmcdonald:</strong></p> <p><strong>You can put your code in this site to convert to vb.</strong></p> <p><a href="http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx">http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx</a></p> 2006-10-09T07:52:02-04:001424047http://forums.asp.net/p/1032944/1424047.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>Thanks for the link I translated the C# code from this site to VB.<br> <a href="http://fredrik.nsquared2.com/viewpost.aspx?PostID=185">http://fredrik.nsquared2.com/viewpost.aspx?PostID=185</a></p> <p>After a bit of monkeying around with the translated code I had this Sub.<font color="#0000ff" size="2"></p> <p>&lt;<font color="#800000" size="2">script</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;&gt;<br> </font><span class="kwd">Sub</span> GridView1_RowCreated(<span class="kwd">ByVal</span> sender <span class="kwd">As Object</span>, <span class="kwd">ByVal</span> e <span class="kwd"> As</span> GridViewRowEventArgs)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">If Not</span> (e.Row <span class="kwd">Is Nothing</span>) <span class="kwd">AndAlso</span> e.Row.RowType = DataControlRowType.Header <span class="kwd"> Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Each</span> cell <span class="kwd">As</span> TableCell <span class="kwd"> In</span> e.Row.Cells<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">If</span> cell.HasControls <span class="kwd">Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim</span> button <span class="kwd">As</span> LinkButton = <span class="kwd"> DirectCast</span>(cell.Controls(0), LinkButton)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">If Not</span> (button <span class="kwd">Is Nothing</span>) <span class="kwd">Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim</span> image <span class="kwd">As</span> Image = <span class="kwd"> New</span> Image<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; image.ImageUrl = <span class="st">&quot;images/default.gif&quot;</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">If</span> GridView1.SortExpression = button.CommandArgument <span class="kwd">Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If</span> GridView1.SortDirection = SortDirection.Ascending <span class="kwd">Then</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; image.ImageUrl = <span class="st">&quot;images/uarrow.gif&quot;</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">Else</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; image.ImageUrl = <span class="st">&quot;images/darrow.gif&quot;</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">End If<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If</span><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cell.Controls.Add(image)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="kwd">End If<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br> &nbsp;&nbsp;&nbsp; End Sub</span><br> &lt;/script&gt;</p> </font> <p></p> <p>&nbsp;&lt;asp: code stuff... Blah!&nbsp;/&gt;</p> <font color="#0000ff" size="2"> <p>&lt;<font color="#800000" size="2">asp</font><font color="#0000ff" size="2">:</font><font color="#800000" size="2">GridView</font><font size="2"> </font><font color="#ff0000" size="2">OnRowCreated</font><font color="#0000ff" size="2">=&quot;GridView1_RowCreated&quot;<font color="#000000"> </font></font><font color="#ff0000" size="2">ID</font><font color="#0000ff" size="2">=&quot;GridView1&quot;</font><font size="2"> </font><font color="#ff0000" size="2">runat</font><font color="#0000ff" size="2">=&quot;server&quot;</font><font size="2"> </font><font color="#ff0000" size="2">AllowSorting</font><font color="#0000ff" size="2">=&quot;True&quot;</font><font size="2"> </font><font color="#0000ff" size="2">&gt;</font></p> </font> <p><font color="#0000ff" size="2">Added &quot;OnRowCreated&quot; and the Sub &quot;GridView1_RowCreated&quot; to the gridview and bam I have sorting arrows for my gridview.<br> Also if your gridview ID is named something other that GridView1 you'll need to change the few references to GridView1&nbsp;in the sub.<br> </font><font color="#0000ff" size="2"><br> I hope this save as much hair as I have pulled out looking for an sorting&nbsp;image solution.&nbsp; :P</font></p> <p><font color="#0000ff" size="2">-Ethan</font></p> <font color="#0000ff" size="2"></font> 2006-10-10T17:06:58-04:001587325http://forums.asp.net/p/1032944/1587325.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>Thanks for your post[:D] and thanks for updating the link to the code in c# and writing it in vb too!</p> <p>&nbsp;I'm doing a C# project, but my co-workers use VB. <br> &nbsp;</p> 2007-02-20T21:13:59-05:001710649http://forums.asp.net/p/1032944/1710649.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>The following code will allow you to have two clickable images within your column header. One indicating ascending and the other indicating descending. A click event on either of the images will reload the page and have it sorted accordingly!</p> <p>&nbsp;</p> <pre class="prettyprint">'--adds a clickable image to indicate the sort direction for sortable columns '--gridview sorting should be enabled for this to work correctly '--within the the GridView_RowCreated Event put the following ' If (e.Row.RowType = DataControlRowType.Header) Then ' AddClickableSortImage(GridView1, e.Row) ' End If ' ' '--create a click event handler for the images 'Sub SortImageClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) ' If Request.QueryString(&quot;col&quot;) &gt; &quot;&quot; Then ' If Request.QueryString(&quot;sort&quot;) = &quot;asc&quot; Then ' GridView1.Sort(Request.QueryString(&quot;col&quot;), SortDirection.Ascending) ' Else ' GridView1.Sort(Request.QueryString(&quot;col&quot;), SortDirection.Descending) ' End If ' End If 'End Sub Protected Sub AddClickableSortImage(ByVal grid As GridView, ByVal item As GridViewRow) Dim i As Integer '--counter Dim col As String '--column name for sorting For i = 0 To grid.Columns.Count - 1 '--iterate through the columns col = grid.Columns(i).SortExpression '--check to make sure this is a sortable column If col &gt; &quot;&quot; Then '--create new image buttons and labels for the header Dim HeaderLabel As New Label Dim BlankLabel As New Label Dim imgBtnUp As New ImageButton Dim imgBtnDown As New ImageButton '--set the properties HeaderLabel.Text = grid.Columns(i).HeaderText &amp; &quot; &quot; BlankLabel.Text = &quot; &quot; '--create a &quot;gap&quot; between sorting images imgBtnUp.PostBackUrl = Request.Path &amp; &quot;?LocID=&quot; &amp; Request.QueryString(&quot;LocID&quot;) &amp; &quot;&amp;col=&quot; &amp; col &amp; &quot;&amp;sort=asc&quot; imgBtnUp.ImageUrl = &quot;~/images/TriangleUp.gif&quot; AddHandler imgBtnUp.Click, AddressOf SortImageClick imgBtnDown.PostBackUrl = Request.Path &amp; &quot;?LocID=&quot; &amp; Request.QueryString(&quot;LocID&quot;) &amp; &quot;&amp;col=&quot; &amp; col &amp; &quot;&amp;sort=desc&quot; imgBtnDown.ImageUrl = &quot;~/images/TriangleDown.gif&quot; AddHandler imgBtnDown.Click, AddressOf SortImageClick '--this will remove the clickable column header link button that is automatically created '--when adding sorting capabilities to a gridview item.Cells(i).Controls.RemoveAt(0) '--add the new controls to the column header item.Cells(i).Controls.Add(HeaderLabel) item.Cells(i).Controls.Add(imgBtnUp) item.Cells(i).Controls.Add(BlankLabel) item.Cells(i).Controls.Add(imgBtnDown) End If Next End Sub '--click event handler for the image buttons for gridview sorting Sub SortImageClick(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) If Request.QueryString(&quot;col&quot;) &gt; &quot;&quot; Then If Request.QueryString(&quot;sort&quot;) = &quot;asc&quot; Then GridView1.Sort(Request.QueryString(&quot;col&quot;), SortDirection.Ascending) Else GridView1.Sort(Request.QueryString(&quot;col&quot;), SortDirection.Descending) End If End If End Sub</pre>&nbsp;&nbsp; 2007-05-15T18:49:30-04:001903259http://forums.asp.net/p/1032944/1903259.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>I know this thread is a little old, but I stumbled across it and it works wonderfully, but I have a little problem&nbsp;I can't seem to work out.</p> <p>&nbsp;My Gridview has a &quot;Filter&quot; dropdown list associated with it, meaning that the user can select an item from the DDL and the GridView will &quot;Filter&quot;, showing only the items relevent to the user's selection (For instance, if &quot;Cars&quot; is selected, only items under a &quot;Cars&quot; category will display).</p> <p>&nbsp;This is the problem:&nbsp; If the GridView is &quot;filtered&quot;, and then a SORT is performed (header clicked), the arrow images show up perfectly, but the GridView reverts back to the original Full List (not the &quot;Filtered&quot; list).&nbsp; Everything was working fine prior to adding this new code for the arrows.</p> <p>Does anyone have any ideas how I can force the gridview to remain filtered??</p> <p>Thanks!</p> 2007-09-11T16:34:26-04:002711768http://forums.asp.net/p/1032944/2711768.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting I am trying c# equivalent code, but this &quot;if (GrideView1.SortExpression == button.CommandArgument)&quot; condition always false because of GrideView1.SortExpression does not have any value. Can you please suggest? protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if ((e.Row != null) &amp;&amp; e.Row.RowType == DataControlRowType.Header) { foreach (TableCell cell in e.Row.Cells) { if (cell.HasControls()) { LinkButton button = (LinkButton)cell.Controls[0]; if ((button != null)) { Image image = new Image(); image.ImageUrl = &quot;images/upArrow1.jpg&quot;; if (GrideView1.SortExpression == button.CommandArgument) { if (GrideView1.SortDirection == SortDirection.Ascending) { image.ImageUrl = &quot;images/upArrow1.jpg&quot;; } else { image.ImageUrl = &quot;images/downArrow1.jpg&quot;; } } cell.Controls.Add(image); } } } } } 2008-10-29T08:26:05-04:003066380http://forums.asp.net/p/1032944/3066380.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>This is a really simple solution and its works like a bomb. Theres a bunch of other solutions on the net for this same problem but they are really over engineered and toooooo complex.</p> <p>&nbsp;Much appreciated</p> <p>Hans <br> </p> 2009-04-06T12:30:32-04:003201592http://forums.asp.net/p/1032944/3201592.aspx/1?Re+Up+Down+GridView+Icon+Image+for+Column+SortingRe: Up Down GridView Icon Image for Column Sorting <p>Another simple&nbsp;solution to enable images as Link for sorting. </p> <font color="#0000ff" size="2"><font color="#0000ff" size="2">protected</font></font><font size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">void</font></font><font size="2"> gvPatientList_RowCreated(</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">object</font></font><font size="2"> sender, </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">GridViewRowEventArgs</font></font><font size="2"> e)</font><font size="2"> <p>{</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">if</font></font><font size="2"> (e.Row != </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">null</font></font><font size="2"> &amp;&amp; e.Row.RowType == </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">DataControlRowType</font></font><font size="2">.Header)</font><font size="2"> <p>{</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">foreach</font></font><font size="2"> (</font><font color="#2b91af" size="2"><font color="#2b91af" size="2">TableCell</font></font><font size="2"> cell </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">in</font></font><font size="2"> e.Row.Cells)</font><font size="2"> <p>{</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">if</font></font><font size="2"> (cell.HasControls())</font><font size="2"> <p>{</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">LinkButton</font></font><font size="2"> button = cell.Controls[0] </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">as</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">LinkButton</font></font><font size="2">;</font><font size="2"> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">HtmlGenericControl</font></font><font size="2"> gv = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">HtmlGenericControl</font></font><font size="2">(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;div&quot;</font></font><font size="2">);</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Label</font></font><font size="2"> lnkName = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Label</font></font><font size="2">();</font><font size="2"> <p>lnkName.Text = button.Text;</p> <p>&nbsp;</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">if</font></font><font size="2"> (button != </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">null</font></font><font size="2">)</font><font size="2"> <p>{</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Image</font></font><font size="2"> imageSort = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Image</font></font><font size="2">();</font><font size="2">imageSort.ImageUrl = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;~/Images/V10Icons/Sort_Up.gif&quot;</font></font><font size="2">;</font><font size="2"> <p>&nbsp;</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">if</font></font><font size="2"> (gvPatientList.SortExpression == button.CommandArgument)</font><font size="2"> <p>{</p> <p></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">if</font></font><font size="2"> (gvPatientList.SortDirection == </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">SortDirection</font></font><font size="2">.Ascending)</p> imageSort.ImageUrl = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;~/Images/V10Icons/Sort_Down.gif&quot;</font></font><font size="2">;</font><font size="2"> <p></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">else</p> </font></font><font size="2">imageSort.ImageUrl = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;~/Images/V10Icons/Sort_Up.gif&quot;</font></font><font size="2">;</font><font size="2"> <p>}</p> <p>gv.Controls.Add(lnkName);</p> <p>gv.Controls.Add(imageSort);</p> <p>button.Controls.Add(gv);</p> <p></font><font color="#008000" size="2"><font color="#008000" size="2">//cell.Controls.Add(imageSort);</p> </font></font><font size="2"> <p>&nbsp;</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> </font> 2009-06-01T10:03:08-04:00