2 Answers, 1 is accepted
0
Princy
Top achievements
Rank 2
answered on 12 Aug 2013, 12:10 PM
Hi Will,
Please try the below code snippet to keep the rows expanded even after updating.
ASPX:
C#:
Thanks,
Princy
Please try the below code snippet to keep the rows expanded even after updating.
ASPX:
<
radG:RadGrid
ID
=
"RadGrid1"
DataSourceID
=
"SqlDataSource1"
runat
=
"server"
Skin
=
"WebBlue"
EnableAJAX
=
"true"
EnableAJAXLoadingTemplate
=
"true"
AutoGenerateEditColumn
=
"true"
AutoGenerateColumns
=
"False"
AllowSorting
=
"True"
AllowMultiRowSelection
=
"true"
AllowAutomaticDeletes
=
"true"
AllowAutomaticInserts
=
"true"
AutoGenerateDeleteColumn
=
"true"
AllowAutomaticUpdates
=
"true"
OnDataBound
=
"RadGrid1_DataBound"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
PagerStyle
Mode
=
"NumericPages"
></
PagerStyle
>
<
MasterTableView
DataSourceID
=
"SqlDataSource1"
DataKeyNames
=
"CustomerID"
Width
=
"100%"
CommandItemDisplay
=
"Top"
>
<
DetailTables
>
<
radG:GridTableView
DataKeyNames
=
"OrderID"
DataSourceID
=
"SqlDataSource2"
Width
=
"100%"
runat
=
"server"
CommandItemDisplay
=
"Top"
PageSize
=
"10"
>
<
ParentTableRelation
>
<
radG:GridRelationFields
DetailKeyField
=
"CustomerID"
MasterKeyField
=
"CustomerID"
/>
</
ParentTableRelation
>
<
Columns
>
<
radG:GridBoundColumn
SortExpression
=
"OrderID"
HeaderText
=
"OrderID"
HeaderButtonType
=
"TextButton"
DataField
=
"OrderID"
UniqueName
=
"OrderID"
>
</
radG:GridBoundColumn
>
<
radG:GridBoundColumn
SortExpression
=
"OrderDate"
HeaderText
=
"Date Ordered"
HeaderButtonType
=
"TextButton"
DataField
=
"OrderDate"
UniqueName
=
"OrderDate"
>
</
radG:GridBoundColumn
>
<
radG:GridBoundColumn
SortExpression
=
"EmployeeID"
HeaderText
=
"EmployeeID"
HeaderButtonType
=
"TextButton"
DataField
=
"EmployeeID"
UniqueName
=
"EmployeeID"
>
</
radG:GridBoundColumn
>
</
Columns
>
</
radG:GridTableView
>
</
DetailTables
>
<
Columns
>
<
radG:GridBoundColumn
SortExpression
=
"CustomerID"
HeaderText
=
"CustomerID"
HeaderButtonType
=
"TextButton"
DataField
=
"CustomerID"
UniqueName
=
"CustomerID"
>
</
radG:GridBoundColumn
>
<
radG:GridBoundColumn
SortExpression
=
"ContactName"
HeaderText
=
"Contact Name"
HeaderButtonType
=
"TextButton"
DataField
=
"ContactName"
UniqueName
=
"ContactName"
>
</
radG:GridBoundColumn
>
<
radG:GridBoundColumn
SortExpression
=
"CompanyName"
HeaderText
=
"Company"
HeaderButtonType
=
"TextButton"
DataField
=
"CompanyName"
UniqueName
=
"CompanyName"
>
</
radG:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
radG:RadGrid
>
C#:
private
Hashtable _ordersExpandedState;
private
Hashtable _selectedState;
public
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
//reset expanded states
this
._ordersExpandedState =
null
;
this
.Session[
"_ordersExpandedState"
] =
null
;
}
}
//Save/load expanded states Hash from the session
//this can also be implemented in the ViewState
private
Hashtable ExpandedStates
{
get
{
if
(
this
._ordersExpandedState ==
null
)
{
_ordersExpandedState =
this
.Session[
"_ordersExpandedState"
]
as
Hashtable;
if
(_ordersExpandedState ==
null
)
{
_ordersExpandedState =
new
Hashtable();
this
.Session[
"_ordersExpandedState"
] = _ordersExpandedState;
}
}
return
this
._ordersExpandedState;
}
}
//Clear the state for all expanded children if a parent item is collapsed
private
void
ClearExpandedChildren(
string
parentHierarchicalIndex)
{
string
[] indexes =
new
string
[
this
.ExpandedStates.Keys.Count];
this
.ExpandedStates.Keys.CopyTo(indexes, 0);
foreach
(
string
index
in
indexes)
{
//all indexes of child items
if
(index.StartsWith(parentHierarchicalIndex +
"_"
) ||
index.StartsWith(parentHierarchicalIndex +
":"
))
{
this
.ExpandedStates.Remove(index);
}
}
}
//Save/load selected states Hash from the session
//this can also be implemented in the ViewState
private
Hashtable SelectedStates
{
get
{
if
(
this
._selectedState ==
null
)
{
_selectedState =
this
.Session[
"_selectedState"
]
as
Hashtable;
if
(_selectedState ==
null
)
{
_selectedState =
new
Hashtable();
this
.Session[
"_selectedState"
] = _selectedState;
}
}
return
this
._selectedState;
}
}
protected
void
RadGrid1_ItemCommand(
object
source, GridCommandEventArgs e)
{
//save the expanded/selected state in the session
if
(e.CommandName == RadGrid.ExpandCollapseCommandName)
{
//Is the item about to be expanded or collapsed
if
(!e.Item.Expanded)
{
//Save its unique index among all the items in the hierarchy
this
.ExpandedStates[e.Item.ItemIndexHierarchical] =
true
;
}
else
//collapsed
{
this
.ExpandedStates.Remove(e.Item.ItemIndexHierarchical);
this
.ClearExpandedChildren(e.Item.ItemIndexHierarchical);
}
}
//Is the item about to be selected
else
if
(e.CommandName == RadGrid.SelectCommandName)
{
//Save its unique index among all the items in the hierarchy
this
.SelectedStates[e.Item.ItemIndexHierarchical] =
true
;
}
//Is the item about to be deselected
else
if
(e.CommandName == RadGrid.DeselectCommandName)
{
this
.SelectedStates.Remove(e.Item.ItemIndexHierarchical);
}
}
protected
void
RadGrid1_DataBound(
object
sender, EventArgs e)
{
//Expand all items using our custom storage
string
[] indexes =
new
string
[
this
.ExpandedStates.Keys.Count];
this
.ExpandedStates.Keys.CopyTo(indexes, 0);
ArrayList arr =
new
ArrayList(indexes);
//Sort so we can guarantee that a parent item is expanded before any of
//its children
arr.Sort();
foreach
(
string
key
in
arr)
{
bool
value = (
bool
)
this
.ExpandedStates[key];
if
(value)
{
RadGrid1.Items[key].Expanded =
true
;
}
}
//Select all items using our custom storage
indexes =
new
string
[
this
.SelectedStates.Keys.Count];
this
.SelectedStates.Keys.CopyTo(indexes, 0);
arr =
new
ArrayList(indexes);
//Sort to ensure that a parent item is selected before any of its children
arr.Sort();
foreach
(
string
key
in
arr)
{
bool
value = (
bool
)
this
.SelectedStates[key];
if
(value)
{
RadGrid1.Items[key].Selected =
true
;
}
}
}
protected
void
grdRebind_Click(
object
sender, EventArgs e)
{
RadGrid1.Rebind();
}
Thanks,
Princy