Solved

How do i  insert a session variable into gridview

Posted on 2008-10-14
6
865 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

830 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