Link to home
Start Free TrialLog in
Avatar of pythonV
pythonV

asked on

How do i insert a session variable into gridview

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.
Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands image

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

Avatar of pythonV
pythonV

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pythonV

ASKER

Thank you, Problem Solved!
Avatar of Nasir Razzaq
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()
Glad to help ^^

JINN