ASPxGridView.FindEditRowCellTemplateControl(GridViewDataColumn, String) Method
Searches for the specified server control contained within the specified cell’s template.
Namespace: DevExpress.Web
Assembly:
DevExpress.Web.v24.2.dll
Declaration
public Control FindEditRowCellTemplateControl(
GridViewDataColumn gridViewDataColumn,
string id
)
Public Function FindEditRowCellTemplateControl(
gridViewDataColumn As GridViewDataColumn,
id As String
) As Control
Parameters
Returns
Type |
Description |
Control |
A Control object that represents the control contained within the specified cell’s template.
|
For more information, see Templates.
Online Example
View Example: How to use cascading combo boxes in inline edit mode
Example
The example shows how to edit a grid column's value with a multi-selection control (CheckBoxList).
The grid displays a person list. A person may belong to different categories. A person's categories are stored as comma separated values. When editing, the grid displays a CheckBoxList with category names to be assigned to a person. This example loads data from the Session.
using System;
using System.Data;
using System.Configuration;
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 System.Collections.Generic;
using DevExpress.Web.Data;
using System.Collections;
using DevExpress.Web;
public partial class _Default : System.Web.UI.Page {
protected List<Person> Persons {
get {
const string key = "DX1";
if(Session[key] == null) {
Session[key] = CreatePersons();
}
return (List<Person>)Session[key];
}
}
protected List<Category> Categories {
get {
const string key = "DX2";
if(Session[key] == null) {
Session[key] = CreateCategories();
}
return (List<Category>)Session[key];
}
}
protected override void OnInit(EventArgs e) {
base.OnInit(e);
Grid.DataSource = Persons;
Grid.DataBind();
}
protected void Grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e) {
Person person = FindPersonById((int)e.Keys[0]);
person.Name = e.NewValues["Name"].ToString();
CheckBoxList list = (CheckBoxList)Grid.FindEditRowCellTemplateControl((GridViewDataColumn)Grid.Columns[2], "List");
if(Grid.IsCallback)
LoadListControlPostDataOnCallback(list);
person.Categories.Clear();
foreach(ListItem item in list.Items) {
if(item.Selected)
person.Categories.Add(FindCategoryByName(item.Value));
}
e.Cancel = true;
Grid.CancelEdit();
}
protected Category FindCategoryByName(string categoryName) {
foreach (Category item in Categories) {
if(item.Name == categoryName)
return item;
}
return null;
}
protected Person FindPersonById(int id) {
foreach(Person item in Persons) {
if(item.ID == id)
return item;
}
return null;
}
protected List<Person> CreatePersons() {
List<Person> persons = new List<Person>();
persons.Add(new Person(1, "Alex", Categories[1], Categories[2]));
persons.Add(new Person(2, "Bill", Categories[0]));
persons.Add(new Person(3, "Kate", Categories[2]));
return persons;
}
protected List<Category> CreateCategories() {
List<Category> categories = new List<Category>();
categories.Add(new Category("Family"));
categories.Add(new Category("Friends"));
categories.Add(new Category("Business"));
return categories;
}
protected void List_DataBound(object sender, EventArgs e) {
CheckBoxList list = (CheckBoxList)sender;
GridViewEditItemTemplateContainer container = (GridViewEditItemTemplateContainer)list.Parent;
IDictionary hash = CreatePersonCategoriesHash(container.Grid.GetRowValues(container.VisibleIndex, "CategoriesString").ToString());
foreach(ListItem item in list.Items)
item.Selected = hash.Contains(item.Value);
}
IDictionary CreatePersonCategoriesHash(string catString) {
Hashtable table = new Hashtable();
string[] names = catString.Split(new string[] { Person.CategorySeparator }, StringSplitOptions.None);
foreach(string name in names)
table.Add(name, null);
return table;
}
// workaround for std ListControl LoadPostData
void LoadListControlPostDataOnCallback(ListControl control) {
if(!Grid.IsEditing) return;
foreach(ListItem item in control.Items)
item.Selected = false;
foreach(string key in Request.Params.AllKeys) {
IPostBackDataHandler dataHandler = control as IPostBackDataHandler;
if(key.StartsWith(control.UniqueID))
dataHandler.LoadPostData(key, Request.Params);
}
}
}
using System;
using System.Data;
using System.Configuration;
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 System.Collections.Generic;
using System.Text;
public class Person {
public const string CategorySeparator = ", ";
int id;
string name;
List<Category> categories;
public Person(int id, string name, params Category[] categories) {
this.id = id;
this.name = name;
this.categories = new List<Category>(categories);
}
public int ID { get { return id; } }
public string Name {
get { return name; }
set { name = value; }
}
public List<Category> Categories { get { return categories; } }
public string CategoriesString {
get {
StringBuilder builder = new StringBuilder();
foreach(Category item in Categories) {
if(builder.Length > 0)
builder.Append(CategorySeparator);
builder.Append(item.Name);
}
return builder.ToString();
}
}
}
<%-- BeginRegion Page setup --%>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="DevExpress.Web.v15.1" namespace="DevExpress.Web" tagprefix="dxwgv" %>
<%-- EndRegion --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>How to use standard ListControl with multi-select feature in the ASPxGridView</title>
</head>
<body>
<form id="form1" runat="server">
<dxwgv:ASPxGridView ID="Grid" runat="server" KeyFieldName="ID" AutoGenerateColumns="false"
OnRowUpdating="Grid_RowUpdating" >
<SettingsEditing Mode="Inline" />
<Columns>
<dxwgv:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True"/>
<dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="1" />
<dxwgv:GridViewDataTextColumn FieldName="CategoriesString" Caption="Categories" VisibleIndex="2" >
<%-- BeginRegion EditItemTemplate --%>
<EditItemTemplate>
<asp:CheckBoxList ID="List" runat="server" DataValueField="Name"
DataSource="<%# Categories %>" OnDataBound="List_DataBound" />
</EditItemTemplate>
<%-- EndRegion --%>
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
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;
public class Category {
string name;
public Category(string name) {
this.name = name;
}
public string Name { get { return name; } }
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports DevExpress.Web.Data
Imports System.Collections
Imports DevExpress.Web
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected ReadOnly Property Persons() As List(Of Person)
Get
Const key As String = "DX1"
If Session(key) Is Nothing Then
Session(key) = CreatePersons()
End If
Return CType(Session(key), List(Of Person))
End Get
End Property
Protected ReadOnly Property Categories() As List(Of Category)
Get
Const key As String = "DX2"
If Session(key) Is Nothing Then
Session(key) = CreateCategories()
End If
Return CType(Session(key), List(Of Category))
End Get
End Property
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
Grid.DataSource = Persons
Grid.DataBind()
End Sub
Protected Sub Grid_RowUpdating(ByVal sender As Object, ByVal e As ASPxDataUpdatingEventArgs)
Dim person As Person = FindPersonById(CInt(Fix(e.Keys(0))))
person.Name = e.NewValues("Name").ToString()
Dim list As CheckBoxList = CType(Grid.FindEditRowCellTemplateControl(CType(Grid.Columns(2), GridViewDataColumn), "List"), CheckBoxList)
If Grid.IsCallback Then
LoadListControlPostDataOnCallback(list)
End If
person.Categories.Clear()
For Each item As ListItem In list.Items
If item.Selected Then
person.Categories.Add(FindCategoryByName(item.Value))
End If
Next item
e.Cancel = True
Grid.CancelEdit()
End Sub
Protected Function FindCategoryByName(ByVal categoryName As String) As Category
For Each item As Category In Categories
If item.Name = categoryName Then
Return item
End If
Next item
Return Nothing
End Function
Protected Function FindPersonById(ByVal id As Integer) As Person
For Each item As Person In Persons
If item.ID = id Then
Return item
End If
Next item
Return Nothing
End Function
Protected Function CreatePersons() As List(Of Person)
Dim persons As List(Of Person) = New List(Of Person)()
persons.Add(New Person(1, "Alex", Categories(1), Categories(2)))
persons.Add(New Person(2, "Bill", Categories(0)))
persons.Add(New Person(3, "Kate", Categories(2)))
Return persons
End Function
Protected Function CreateCategories() As List(Of Category)
Dim categories As List(Of Category) = New List(Of Category)()
categories.Add(New Category("Family"))
categories.Add(New Category("Friends"))
categories.Add(New Category("Business"))
Return categories
End Function
Protected Sub List_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim list As CheckBoxList = CType(sender, CheckBoxList)
Dim container As GridViewEditItemTemplateContainer = CType(list.Parent, GridViewEditItemTemplateContainer)
Dim hash As IDictionary = CreatePersonCategoriesHash(container.Grid.GetRowValues(container.VisibleIndex, "CategoriesString").ToString())
For Each item As ListItem In list.Items
item.Selected = hash.Contains(item.Value)
Next item
End Sub
Private Function CreatePersonCategoriesHash(ByVal catString As String) As IDictionary
Dim table As New Hashtable()
Dim names() As String = catString.Split(New String() { Person.CategorySeparator }, StringSplitOptions.None)
For Each name As String In names
table.Add(name, Nothing)
Next name
Return table
End Function
' workaround for std ListControl LoadPostData
Private Sub LoadListControlPostDataOnCallback(ByVal control As ListControl)
If (Not Grid.IsEditing) Then
Return
End If
For Each item As ListItem In control.Items
item.Selected = False
Next item
For Each key As String In Request.Params.AllKeys
Dim dataHandler As IPostBackDataHandler = TryCast(control, IPostBackDataHandler)
If key.StartsWith(control.UniqueID) Then
dataHandler.LoadPostData(key, Request.Params)
End If
Next key
End Sub
End Class
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Public Class Category
Private name_Renamed As String
Public Sub New(ByVal name As String)
Me.name_Renamed = name
End Sub
Public ReadOnly Property Name() As String
Get
Return name_Renamed
End Get
End Property
End Class
<%-- BeginRegion Page setup --%>
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register assembly="DevExpress.Web.v15.1" namespace="DevExpress.Web" tagprefix="dxwgv" %>
<%-- EndRegion --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>How to add ListControl (CheckBoxList) in EditItemTemplate</title>
</head>
<body>
<form id="form1" runat="server">
<dxwgv:ASPxGridView ID="Grid" runat="server" KeyFieldName="ID" AutoGenerateColumns="false"
OnRowUpdating="Grid_RowUpdating" >
<SettingsEditing Mode="Inline" />
<Columns>
<dxwgv:GridViewCommandColumn VisibleIndex="0" ShowEditButton="True"/>
<dxwgv:GridViewDataTextColumn FieldName="Name" VisibleIndex="1" />
<dxwgv:GridViewDataTextColumn FieldName="CategoriesString" Caption="Categories" VisibleIndex="2" >
<%-- BeginRegion EditItemTemplate --%>
<EditItemTemplate>
<asp:CheckBoxList ID="List" runat="server" DataValueField="Name" DataSource="<%#Categories%>" OnDataBound="List_DataBound" />
</EditItemTemplate>
<%-- EndRegion --%>
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.Text
Public Class Person
Public Const CategorySeparator As String = ", "
Private id_Renamed As Integer
Private name_Renamed As String
Private categories_Renamed As List(Of Category)
Public Sub New(ByVal id As Integer, ByVal name As String, ParamArray ByVal categories() As Category)
Me.id_Renamed = id
Me.name_Renamed = name
Me.categories_Renamed = New List(Of Category)(categories)
End Sub
Public ReadOnly Property ID() As Integer
Get
Return id_Renamed
End Get
End Property
Public Property Name() As String
Get
Return name_Renamed
End Get
Set(ByVal value As String)
name_Renamed = value
End Set
End Property
Public ReadOnly Property Categories() As List(Of Category)
Get
Return categories_Renamed
End Get
End Property
Public ReadOnly Property CategoriesString() As String
Get
Dim builder As New StringBuilder()
For Each item As Category In Categories
If builder.Length > 0 Then
builder.Append(CategorySeparator)
End If
builder.Append(item.Name)
Next item
Return builder.ToString()
End Get
End Property
End Class
The following code snippets (auto-collected from DevExpress Examples) contain references to the FindEditRowCellTemplateControl(GridViewDataColumn, String) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also