Solved

How do i  insert a session variable into gridview

Posted on 2008-10-14
6
868 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22718667
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
ID: 22719077
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
ID: 22719105
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pythonV
ID: 22719128
Thank you, Problem Solved!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22719131
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
ID: 22719139
Glad to help ^^

JINN
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

729 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