Solved

How do i  insert a session variable into gridview

Posted on 2008-10-14
6
859 Views
Last Modified: 2012-05-05
Hello,

I am using ASP.NET and VB.NET, I have a gridview wconnected to a table adapter which returns 3 columns from a database. Column 1 is a feature ID, column 2 is a description and column 3 is detail.

Can I in some way insert a Session variable into a cell in Column 3 where ID = 3 for example.

thanks, much appreciated.
0
Comment
Question by:pythonV
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
Hi,

You can interfere anytime in anyway your gridview bind its data, and manipulate the way you want it.

Set the event RowDataBound of your gridview (this event will be fired when datarow is bounded, your can set how you want the specific cell to display. You use VB right? So please take a look at the example:

<asp:gridview id = "myGridView" runat = "server" onRowDataBound = "..." ... > (you better get this on your design)

For more information please visit
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Hope this helps
JINN

Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
 

    If e.Row.RowType = DataControlRowType.DataRow Then
 

      ' Get your session variable and display here.

      e.Row.Cells(2).Text = "<i>" & Session("MySessionValue") & "</i>"
 

    End If
 

  End Sub

Open in new window

0
 

Author Comment

by:pythonV
Comment Utility
thanks for the response,however, what this is doing is filling all the cells in the column with the same variable. I want just 1 specific cell.

thanks.
0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
Comment Utility
Put one more if statement in, to check the row index (to see if the row match your condition then change it).This wont slow down the performance (very very little effect).

If e.Row.RowType = DataControlRowType.DataRow Then
      if e.Row.Cell(0).Text = "3" then    'or equals your wanted ID
         ' Get your session variable and display here.
         e.Row.Cells(2).Text = "<i>" & Session("MySessionValue") & "</i>"
      End if
 
    End If

Hope this help
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:pythonV
Comment Utility
Thank you, Problem Solved!
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
If you want to insert that value after the grid has been populated then you need to update the datatable which is the source of the grid. If you are using dataset or datatable to populate the grid, you could insert the value into table and then refresh the grid. For example

dTable.DefaultView.RowFilter="FeatureID=3"
if dTable.DefaultView.Count>0 then
   dTable.DefaultView.Item(0).Item(2)="SomeValue"
end if
dTable.DefaultView.RowFilter=""
dGrid.DataBind()
0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
Glad to help ^^

JINN
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now