Requirements |
||
RadControls version |
|
|
.NET version | 2.0 | |
Visual Studio version | 2005 | |
programming language | C#, Javascript | |
browser support |
all browsers supported by RadControls |
PROJECT DESCRIPTION
This project illustrates how to preform Grouping/UnGrouping action in RadGrid on double clicking the column header. The task is achieved by adding an OnColumnDblClick client event for the Grid and performing a PostBack from the client side. Then in the RaisePostBack event Grouping/UnGrouping can be performed.
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title>Untitled Page</title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<script type="text/javascript" language="javascript"> |
function OnColumnDblClick(sender, eventArgs) |
{ |
__doPostBack("<%= RadGrid1.UniqueID %>", "ColumnDblClicked:" + eventArgs.get_gridColumn().get_uniqueName()); |
} |
</script> |
<asp:ScriptManager ID="ScriptManager1" runat="server" /> |
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>" |
SelectCommand="SELECT [ProductName], [SupplierID], [CategoryID] FROM [Products]"> |
</asp:SqlDataSource> |
<br /> |
<br /> |
<div> |
<telerik:RadGrid ID="RadGrid1" runat="server" ShowGroupPanel="true" DataSourceID="SqlDataSource1" GridLines="None" |
Skin="Office2007"> |
<MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> |
<RowIndicatorColumn Visible="False"> |
<HeaderStyle Width="20px" /> |
</RowIndicatorColumn> |
<ExpandCollapseColumn Resizable="False" Visible="False"> |
<HeaderStyle Width="20px" /> |
</ExpandCollapseColumn> |
<Columns> |
<telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" |
UniqueName="ProductName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="SupplierID" DataType="System.Int32" HeaderText="SupplierID" |
SortExpression="SupplierID" UniqueName="SupplierID"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="CategoryID" DataType="System.Int32" HeaderText="CategoryID" |
SortExpression="CategoryID" UniqueName="CategoryID"> |
</telerik:GridBoundColumn> |
</Columns> |
</MasterTableView> |
<ClientSettings > |
<ClientEvents OnColumnDblClick="OnColumnDblClick" /> |
</ClientSettings> |
</telerik:RadGrid></div> |
</form> |
</body> |
</html> |
CS:
using System; |
using System.Data; |
using System.Configuration; |
using System.Collections; |
using System.Web; |
using System.Web.Security; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using System.Web.UI.WebControls.WebParts; |
using System.Web.UI.HtmlControls; |
using Telerik.Web.UI; |
public partial class _Default : System.Web.UI.Page |
{ |
protected void Page_Load(object sender, EventArgs e) |
{ |
} |
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument) |
{ |
base.RaisePostBackEvent(source, eventArgument); |
if (source == this.RadGrid1 && eventArgument.IndexOf("ColumnDblClicked") != -1) |
{ |
string strUniqueName = eventArgument.Split(':')[1]; |
ArrayList arrClickedColumn; |
if (Session["arrClickedColumn"] == null) |
{ |
arrClickedColumn = new ArrayList(); |
} |
else |
{ |
arrClickedColumn = (ArrayList)Session["arrClickedColumn"]; |
} |
if (!arrClickedColumn.Contains(strUniqueName)) |
arrClickedColumn.Add(strUniqueName); |
Session["arrClickedColumn"] = arrClickedColumn; |
if (RadGrid1.MasterTableView.GroupByExpressions.Count > 0) |
{ |
bool newGroup = true; |
int GroupIndex; |
for (GroupIndex = 0; GroupIndex < RadGrid1.MasterTableView.GroupByExpressions.Count; GroupIndex++) |
{ |
if (RadGrid1.MasterTableView.GroupByExpressions[GroupIndex].Expression.Contains(strUniqueName)) |
newGroup = false; |
} |
if (Session["arrClickedColumn"] != null) |
{ |
ArrayList arrColumns = (ArrayList)Session["arrClickedColumn"]; |
int arrColumnCount; |
if (newGroup == true) |
arrColumnCount = arrColumns.Count - 1; |
else |
arrColumnCount = arrColumns.Count; |
int columnCount; |
for (columnCount = 0; columnCount < arrColumnCount; columnCount++) |
{ |
if (arrColumns[columnCount].ToString() == strUniqueName) |
{ |
string strGrpexp = strUniqueName + " " + "Group By" + " " + strUniqueName; |
RadGrid1.MasterTableView.GroupByExpressions.Remove(strGrpexp); |
arrClickedColumn.Remove(strUniqueName); |
strUniqueName = string.Empty; |
} |
else if (newGroup) |
{ |
AddGroup(strUniqueName); |
arrClickedColumn.Remove(strUniqueName); |
strUniqueName = string.Empty; |
} |
} |
} |
} |
else |
{ |
AddGroup(strUniqueName); |
} |
RadGrid1.Rebind(); |
} |
} |
private void AddGroup(string strUniqueName) |
{ |
if (strUniqueName != String.Empty) |
{ |
GridGroupByExpression expression = new GridGroupByExpression(); |
GridGroupByField gridGroupByField = new GridGroupByField(); |
gridGroupByField = new GridGroupByField(); |
gridGroupByField.FieldName = strUniqueName; |
gridGroupByField.HeaderText = strUniqueName; |
expression.SelectFields.Add(gridGroupByField); |
expression.GroupByFields.Add(gridGroupByField); |
RadGrid1.MasterTableView.GroupByExpressions.Add(expression); |
} |
} |
} |