Rick
asked on
asp.net - GridView columns to session object
Hello,
I'm adding columns to a GridView in code:
Dim bf0 As BoundField = New BoundField()
..., etc
With bf0
.HeaderText = "SKU"
.DataField = "SKU"
.HtmlEncode = False
.SortExpression = "SKU"
.ItemStyle.HorizontalAlign = HorizontalAlign.Left
.ItemStyle.Wrap = False
End With
..., etc
With GridView1.Columns
.Add(bf0)
..., etc
End With
Is there a way to save these columns in a session object, then on postback add them back to the GridView?
I tried:
Session("GridViewColumns") = GridView1.Columns
Then:
GridView1.Columns.Add(Sess ion("GridV iewColumns "))
... but this gives me "Value cannot be null. Parameter name: value"
Thank you.
I'm adding columns to a GridView in code:
Dim bf0 As BoundField = New BoundField()
..., etc
With bf0
.HeaderText = "SKU"
.DataField = "SKU"
.HtmlEncode = False
.SortExpression = "SKU"
.ItemStyle.HorizontalAlign
.ItemStyle.Wrap = False
End With
..., etc
With GridView1.Columns
.Add(bf0)
..., etc
End With
Is there a way to save these columns in a session object, then on postback add them back to the GridView?
I tried:
Session("GridViewColumns")
Then:
GridView1.Columns.Add(Sess
... but this gives me "Value cannot be null. Parameter name: value"
Thank you.
Why would you want to? Normally you would simply re-add the columns on postback. If you're code to add the columns is in it's own method then you only need to call it.
For this generate dynamic grid differenrtky
the following link will explain way of dynamic gridview generation.
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
http://forums.asp.net/t/1020163.aspx
http://www.geekinterview.com/question_details/23647
see the links. in this links datatable is assigned as datasource to gridview. place this datatable in Session object.
Session("Grid") = dt;
the following link will explain way of dynamic gridview generation.
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
http://forums.asp.net/t/1020163.aspx
http://www.geekinterview.com/question_details/23647
see the links. in this links datatable is assigned as datasource to gridview. place this datatable in Session object.
Session("Grid") = dt;
You could also just put the entire GridView in the Session which might be easier. Do you need to keep it even after they leave the page? If not, the ViewState might be better than the Session.
ASKER
I want to do this because my gridview has over 100 columns... and it takes a couple of seconds to create and setup all boundfields.
Right now, yes, on postback I call the method that adds the columns to the gridview...
I was wondering if I created the columns on first pass and stored them in a session object... would it not be faster to just grab them from the object rather than re-creating all columns?
Thank you.
Right now, yes, on postback I call the method that adds the columns to the gridview...
I was wondering if I created the columns on first pass and stored them in a session object... would it not be faster to just grab them from the object rather than re-creating all columns?
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The columns are not user specific.
How do I cache them? ... and how do I retrieve them on postback?
How do I cache them? ... and how do I retrieve them on postback?
No need caching should be done.
Based on user get those columns and bind to the grid view, that will be easy..
Read articles which i have posted in my earlier comment, those are exactly what you need.
instead of putting grid view in the session put the datatable in session and assign to the grideview in evry postback. that will be very easy and faster.
Based on user get those columns and bind to the grid view, that will be easy..
Read articles which i have posted in my earlier comment, those are exactly what you need.
instead of putting grid view in the session put the datatable in session and assign to the grideview in evry postback. that will be very easy and faster.
jagssidurala is correct - I should have said to store the datatable, not the gridview. Here is a small, simplified example. Anytime you change the datatable
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Do some stuff to generate the datatable
DdSourceTable = dt; // this line saves the datatable
}
RebindGridView(); rebinds the table from the Session
}
private void RebindGridView()
{
gvFileList.DataSource = DdSourceTable; // this line retrieves the datatable
gvFileList.DataBind();
}
// This property stores and retrives the database in the Session
private DataTable DdSourceTable
{
get { return Session["A_UNIQUE_STRING"] as DataTable ?? null; }
set { Session["A_UNIQUE_STRING"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Do some stuff to generate the datatable
DdSourceTable = dt; // this line saves the datatable
}
RebindGridView(); rebinds the table from the Session
}
private void RebindGridView()
{
gvFileList.DataSource = DdSourceTable; // this line retrieves the datatable
gvFileList.DataBind();
}
// This property stores and retrives the database in the Session
private DataTable DdSourceTable
{
get { return Session["A_UNIQUE_STRING"]
set { Session["A_UNIQUE_STRING"]
}
What I meant to say: anytime you change the datatable put it back in the session like this: DdSourceTable = dt;
And the same concept would also work for ViewState or Application objects depending on the scope of the data you are displaying in the table.
Ok maybe i'm missing something. What exactly has caching the DataTable (something you really don't want to be doing by the way) go to do with adding custom columns??
There is no need to place the columns in session. The boundedfield can be added in the aspx page (at design tme) or at runtime. whatever you do, viewstate holds the control.
protected void Page_Load(object sender, EventArgs e)
{
GridView1.AutoGenerateColu mns = false;
if(GridView1.Columns.Count ==0 && !Page.IsPostBack)
GridView1.Columns.Add(Crea teColumn() );
BindData();
}
protected void btnTest_Click(object sender, EventArgs e)
{
}
private BoundField CreateColumn()
{
BoundField bfield = new BoundField();
bfield.HeaderText="SKU";
bfield.DataField = "SKU";
bfield.HtmlEncode = false;
bfield.SortExpression = "SKU";
bfield.ItemStyle.Horizonta lAlign = HorizontalAlign.Left;
bfield.ItemStyle.Wrap = false;
return bfield;
}
private void BindData()
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
DataColumn dc = new DataColumn();
dc.ColumnName = "SKU";
dt.Columns.Add(dc);
dr[0] = "sdfsdf";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.AutoGenerateColu
if(GridView1.Columns.Count
GridView1.Columns.Add(Crea
BindData();
}
protected void btnTest_Click(object sender, EventArgs e)
{
}
private BoundField CreateColumn()
{
BoundField bfield = new BoundField();
bfield.HeaderText="SKU";
bfield.DataField = "SKU";
bfield.HtmlEncode = false;
bfield.SortExpression = "SKU";
bfield.ItemStyle.Horizonta
bfield.ItemStyle.Wrap = false;
return bfield;
}
private void BindData()
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
DataColumn dc = new DataColumn();
dc.ColumnName = "SKU";
dt.Columns.Add(dc);
dr[0] = "sdfsdf";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}