Get a Clicked Column Index Integer value for a GridView column using the Sorting Event Handler

I am creating a GridView nested within a UpdatePanel and I got the sorting, paging all down to a sciene using the GridView events handlers. Now, I stumbled to a minor problem that I can't seem to figure out how to go about it. When sorting, I would like the heading color of the clicked sorted column to change (using CSS). I can't seem to dynamically pass the value of the clicked column index to the method. I am using the following reference to change the color of the column but I cannot seem to retreive the value of the clicked index in integer value:


 GridView1.Columns(trying to get the index of the clicked column here).HeaderStyle.BackColor = Drawing.Color.CadetBlue

This is my current method:

 

 Protected Sub GridViewIndexing(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles GridView1.Sorting

Dim strExpression As String = e.SortExpression.ToString()
Dim strDir As String = ""
	'This is working, but as you notice, the column index value is hardcoded...
GridView1.Columns(0).HeaderStyle.BackColor = Drawing.Color.CadetBlue

Dim strSessionFlag As String = "" If Not Me.Context.Session.Item("GridSession") Is Nothing Then strSessionFlag = Me.Context.Session.Item("GridSession").ToString
End If

Dim
CachedDt As DataTable = New DataTable

Try

If
strSessionFlag <> "" Then
If
strSessionFlag = "Ascending" Then
Me
.Context.Session.Item("GridSession") = "Descending" strDir = "Desc" ElseIf strSessionFlag = "Descending" Then
Me
.Context.Session.Item("GridSession") = "Ascending" strDir = "Asc" End If
Else
strDir = "Asc" Me.Context.Session.Item("GridSession") = e.SortDirection.ToString()
End If

If Not
Cache.Item("CachedDT") Is Nothing Then CachedDt = CType(Cache.Item("CachedDT"), DataTable)
CachedDt.DefaultView.Sort = strExpression + " " + strDir
GridView1.DataSource = CachedDt
GridView1.DataBind()
Else Cache.Insert("CachedDT", AddDT())
CachedDt = CType(Cache.Item("CachedDT"), DataTable)
CachedDt.DefaultView.Sort = strExpression + " " + strDir
GridView1.DataSource = CachedDt
GridView1.DataBind()
End If
Catch
ex As NullReferenceException
lblErrorMsg.Text = ex.ToString
Catch ex As Exception
lblErrorMsg.Text = ex.ToString
End Try

End Sub

  

 

Any suggestions? 


Victor A. Palma
0
vicpal25
9/10/2007 3:41:35 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

1 Replies
860 Views

Similar Articles

[PageSpeed] 45

Hi:

  You can try this:

  Dim name As String


    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        name = e.SortExpression 'Register the SortExpression

    End Sub

  
    Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender
        For Each tc As TableCell In GridView1.HeaderRow.Cells
            Dim l As LinkButton = tc.Controls(0)
            If l.Text = name Then 'Find the correct TableCell
                tc.BackColor = Drawing.Color.AliceBlue

 Return

            End If
        Next
    End Sub

 <asp:GridView AllowSorting="true" ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
        </asp:GridView>

Regards


Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Allen
9/12/2007 1:49:55 AM
Reply:

Similar Artilces:

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

How do you get a column value from a gridview column
How do you get a column value from a gridview column in asp.net 3.5? I tried this but it does not work.Dim myRow As GridViewRowFor Each myRow In GridView1.Rows    Dim strTest As String = DataBinder.Eval(myRow.DataItem, "EmailSubject")Nextif I change the column to a Template field then EmailSubject = myRow.FindControl("EmailSubject") works but I was hoping for a more direct way of getting a column value EmailSubject = myRow.Cells(4).Text also works but is not flexible if I change the column orderThanks Craig Try: Dim strTest As String = CType(...

How to get GridView column values while looping through the columns?
Hi everybody, I'm trying to figure out how can I get values from DataFields while  looping through the Grid rows. Here is what I'm trying to do:GridViewRow row = null; for (int i = 0; i < this.ProfilesGrid.Rows.Count; i++ ) {row = this.ProfilesGrid.Rows[i];   CheckBox chkbtnApproved = row.FindControl("chkApproved") as CheckBox ;if (chkbtnApproved != null) {if (chkbtnApproved.Checked == true) {int RegID = (int)this.ProfilesGrid.DataKeys[i].Value ;   //PersonName = row.DataItem["FirstName"] + " " + row.DataItem["LastNam...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

Reference gridview column index using column headers
 Greetings,I have several GridViews all showing different collections of similar product data.  Due to changes in codes used to distinguish properties such as color and size, I'm having to write several rules into the row databound events.  The issue I'm having is that the group who will be using the application haven't finalized the order of the columns, so everytime I reorder the columns, I have to changes all the column indexes in my code behind so that the rules are being applied to the right columns. I'd like to know if there is a way to reference the colu...

Getting column value from a gridview control
I have a grid view control and has one datakey and other columns. In my grid view I have an Edit Control and a Delete Control. 1) When I press Edit control, I am getting datakey value as expected. But I am not getting other column name i.e. pdf_filename 2) when I press Delete control, I should get the datakey value. But I am not getting I am using c#  Please help me Hi vmk, Could you be a littlemore elaborate? like how have you bound your grid? 1.Columns autogenerated 2.Using TemplateFields 3.Using BoundFields.Regards,NaveenPlease remember to click Mark as Answer on the pos...

Hide a Gridview column but use the value in that column when the row is selected.
I need to retrieve a column from my database but do not want to display it in the gridview.  However, I need to be able to show that information later in the application.  I am using the wizard control.  I believe I can use the DataKeyValues property of the gridview but can't figure out how to write the code to get the value of the cell in that column when the row is selected so it can be referenced later.  I am using VB.  Thanks for your help!! Found the answer.  Here it is in case anyone else needs it:       &nbs...

Hiding columns in the ItemDataBound event without using the column index
Lately I've needed to hide certain columns in DataGrids depending on the specific data source so I create a property at the top of the page like so: private string DisplayProjDesc //DisplayProjDesc property - for ItemDataBound event of the DataGrids to determine whether to display project description { set { this.lblBoolDispProjDesc.Text = value; } get { return this.lblBoolDispProjDesc.Text.ToUpper(); } }Then, in the ItemDataBound event of the DatGrid, I use something like: if(this.DisplayProjDesc=="TRUE") { this.dgProject.Columns[7].Visible=true; } else { this.dgProject....

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

Get Value from Hidden Column in Gridview control
I have managed to get the value from my hidden field by using the SelectedIndexChanged event and the following line of code:string keyValue = GridView1.SelectedDataKey.Value.ToString()I would however prefer to get access to the hidden column value on the GridView1_RowDataBound event, so that I can then evaluate each row against the hidden columns value and decide whether or not to display the edit button in each row. Can I do this a different way? You can use e.Row.DataItem("field name") in the RowDataBound event -  as long as the field is in your SQL statement doesn't m...

How to access GridView hidden Column value by clicking a template column button?
Hi: My GridView contains(for simplicity sake) three columns: a hiden template column that contains the DocID (the DataKey), a template column that contains DocTitle and a template button column that contains a button called btnDetails. When the GridView is loaded with data, the user may click the button on a row and obtain the DocID from the hidden column of that row. This seems to be simple enough. However, when the button is clicked, the GridView always returns blank. Any idea why this happens? My code is as follows: ASPX <asp:GridView ID="gvList" runat=&...

How I sort the gridview column after column
I have the following columns in my gridview(VB) Type   FirstName   LastName    Company   City How I can sort the gridview firstly by column 1, after that by column 2 and so on. Thanks in advance.   You can write your SQL SELECT statement this way: SELECT Type ,  FirstName,   LastName,    Company,   City  FROM yourtable ORDER BY 1 ASC, 2, 3,4, 5 OR SELECT Type,  FirstName,   LastName,    Company,   City  FROM yourtable ORDER BY  Type&...

How to set visible property of Gridview columns using column name instead of index
How to  set visible property of Gridview  columns using column name instead of index Dear janacpp, the columns collection of gridview has no indexer for column name. So you can do something like that:  in your code section you want to set the visible property add this code : GridView1.Columns[GridView1.Columns.IndexOf(GetField("FieldName"))].Visible=false;  where FieldName is the name of your field. and add this function:DataControlField GetField(string name) {DataControlFieldCollection fields = GridView1.Columns.CloneFields();for (int i = 0; i < f...

Get value from current Gridview column on button click
 I am searching for the answer to this, should be easy, but I just can't get it.I have a Gridview.  I placed a button column on it.  Now each row has a button.  When they click the button I want to grab the value of one of the columns and do some stuff.  I made it a template field with a button, on in the on_click of that button I put:myString = me.GridView1.SelectedRow.Cells("mycolumn").textThat said it was a nullRefernceException.  I know there is data in that column.  So I tried myString = me.GridView1.SelectedRow.Cells(1).textGot the ...

Web resources about - Get a Clicked Column Index Integer value for a GridView column using the Sorting Event Handler - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 12/16/2015 7:48:01 PM