Skip to content

Commit 46f8f30

Browse files
committed
docs(grid): added new examples and sections
1 parent eff08d0 commit 46f8f30

File tree

1 file changed

+154
-18
lines changed

1 file changed

+154
-18
lines changed

controls/grid/how-to/Common/using-the--getitems-getcolumn-and-getcolumnsafe-methods.md

Lines changed: 154 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,178 @@ position: 5
1111
# Using the GetItems GetColumn and GetColumnSafe Methods
1212

1313

14+
In various cases you may want to access or traverse grid functional items/columns outside the dedicated ItemCreated and ItemDataBound event handlers, for example during PreRender or DataBinding event handlers of the grid. This is easily attainable with the **GetItems(itemType), GetColumn(columnName)** and **GetColumnSafe(columnName)** methods (exposed by each **GridTableView** instance).
1415

15-
##
16+
The topic demonstrates accessing the following instances:
17+
* [Items](#items)
18+
* [Edit Items](#edit-items)
19+
* [Insert Item](#insert-item)
20+
* [Columns](#columns)
1621

17-
In various cases you may want to access grid functional items/columns outside of the grid server event handlers. This is easily attainable with the **GetItems(itemType), GetColumn(columnName)** and **GetColumnSafe(columnName)** methods (exposed by each **GridTableView** instance).
18-
19-
The **GetItems(itemType)** method returns an array of items (in the respective GridTableView) which match the specified type. You can use the **GridItemType** enumeration to choose the item type, for example:
22+
**Batch editing mode** is highly client-side based and differs from EditForms, PopUp and InPlace modes. It requires specific handling, which is explained in the [Accessing Cells And Rows]({%slug grid/rows/accessing-cells-and-rows%}) article.
2023

24+
## Items
2125

26+
The **GetItems(itemType)** method returns an array of items (in the respective GridTableView) which match the specified type. You can use the **GridItemType** enumeration to choose the item type, for example:
2227

28+
````C#
29+
GridItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
30+
//cast the item to GridHeaderItem and operate with it further
31+
````
2332
````VB
2433
Dim headerItem As GridItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)(0)
2534
'cast the item to GridHeaderItem and operate with it further
2635
````
36+
37+
Data items loaded on the current page can be traversed using the following approach:
38+
2739
````C#
28-
GridItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
29-
//cast the item to GridHeaderItem and operate with it further
40+
protected void RadGrid1_PreRender(object sender, EventArgs e)
41+
{
42+
foreach (GridDataItem item in RadGrid1.Items)
43+
{
44+
if (item.ItemIndex % 3 == 0)
45+
{
46+
item["ShipName"].BackColor = System.Drawing.Color.Orange;
47+
}
48+
}
49+
}
50+
````
51+
````VB
52+
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
53+
For Each item As GridDataItem In RadGrid1.Items
54+
If item.ItemIndex Mod 3 = 0 Then
55+
item("ShipName").BackColor = System.Drawing.Color.Orange
56+
End If
57+
Next
58+
End Sub
3059
````
3160

61+
## Edit Items
3262

33-
The **GetColumn(columnName)** method returns the column with the unique name specified as argument, namely:
63+
Currently edited items can be accessed using one of the following methods:
3464

65+
````C#
66+
protected void RadGrid1_PreRender(object sender, EventArgs e)
67+
{
68+
foreach (GridDataItem item in RadGrid1.EditItems)
69+
{
70+
// InPlace EditMode
71+
TextBox textBox1 = item["ShipName"].Controls[0] as TextBox;
3572

73+
// EditForms and PopUp EditMode
74+
GridEditFormItem editFormItem = item.EditFormItem;
75+
if (editFormItem.IsInEditMode)
76+
{
77+
TextBox textBox2 = editFormItem["ShipName"].Controls[0] as TextBox;
78+
}
79+
}
3680

81+
// alternative approach
82+
foreach (GridEditFormItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
83+
{
84+
if (item.IsInEditMode)
85+
{
86+
TextBox textBox3 = item["ShipName"].Controls[0] as TextBox;
87+
}
88+
}
89+
}
90+
````
3791
````VB
38-
Dim column As GridColumn = RadGrid1.MasterTableView.GetColumn("OrderID")
39-
'thus you get reference to the column with OrderID unique name
92+
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
93+
For Each item As GridDataItem In RadGrid1.EditItems
94+
' InPlace EditMode
95+
Dim textBox1 As TextBox = TryCast(item("ShipName").Controls(0), TextBox)
96+
97+
' EditForms and PopUp EditMode
98+
Dim editFormItem As GridEditFormItem = item.EditFormItem
99+
If editFormItem.IsInEditMode Then
100+
Dim textBox2 As TextBox = TryCast(editFormItem("ShipName").Controls(0), TextBox)
101+
End If
102+
Next
103+
104+
' alternative approach
105+
For Each item As GridEditFormItem In RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)
106+
If item.IsInEditMode Then
107+
Dim textBox3 As TextBox = TryCast(item("ShipName").Controls(0), TextBox)
108+
End If
109+
Next
110+
End Sub
40111
````
112+
113+
## Insert Item
114+
115+
The Insert Item can be accessed using the following method:
116+
117+
````C#
118+
protected void RadGrid1_PreRender(object sender, EventArgs e)
119+
{
120+
if (RadGrid1.MasterTableView.IsItemInserted)
121+
{
122+
// GridEditFormInsertItem for EditForms and PopUp EditMode
123+
// GridDataInsertItem for InPlace EditMode
124+
GridEditableItem insertItem = RadGrid1.MasterTableView.GetInsertItem();
125+
}
126+
}
127+
````
128+
````VB
129+
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
130+
If RadGrid1.MasterTableView.IsItemInserted Then
131+
' GridEditFormInsertItem for EditForms and PopUp EditMode
132+
' GridDataInsertItem for InPlace EditMode
133+
Dim insertItem As GridEditableItem = RadGrid1.MasterTableView.GetInsertItem()
134+
End If
135+
End Sub
136+
````
137+
138+
## Columns
139+
140+
The **GetColumn(columnName)** method returns the column with the unique name specified as argument, namely:
141+
41142
````C#
42143
GridColumn column = RadGrid1.MasterTableView.GetColumn["OrderID"];
43144
//thus you get reference to the column with OrderID unique name
44145
````
45-
146+
````VB
147+
Dim column As GridColumn = RadGrid1.MasterTableView.GetColumn("OrderID")
148+
'thus you get reference to the column with OrderID unique name
149+
````
46150

47151
The **GetColumnSafe(columnName)** performs the same task as **GetColumn(columnName)** method, however **GetColumnSafe** will not raise an exception in case column with that name is not found in the corresponding **GridTableView**.
48152

153+
The entire column collection can be traversed using the following approach:
154+
155+
````C#
156+
protected void RadGrid1_DataBinding(object sender, EventArgs e)
157+
{
158+
foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)
159+
{
160+
if (col.DataType == typeof(decimal))
161+
{
162+
(col as IGridDataColumn).AllowFiltering = false;
163+
}
164+
if (col is GridDateTimeColumn)
165+
{
166+
GridDateTimeColumn dateCol = (GridDateTimeColumn)col;
167+
dateCol.DataFormatString = "{0:d}";
168+
}
169+
}
170+
}
171+
````
172+
````VB
173+
Protected Sub RadGrid1_DataBinding(sender As Object, e As EventArgs)
174+
For Each col As GridColumn In RadGrid1.MasterTableView.RenderColumns
175+
If col.DataType = GetType(Decimal) Then
176+
TryCast(col, IGridDataColumn).AllowFiltering = False
177+
End If
178+
If TypeOf col Is GridDateTimeColumn Then
179+
Dim dateCol As GridDateTimeColumn = DirectCast(col, GridDateTimeColumn)
180+
dateCol.DataFormatString = "{0:d}"
181+
End If
182+
Next
183+
End Sub
184+
````
185+
49186
The forthcoming sample implementation is made for hierarchical grid with two levels. With separate buttons on the page you can:
50187

51188
* switch the visibility for the inner tables command item link button (only for expanded parent items)
@@ -104,12 +241,12 @@ The forthcoming sample implementation is made for hierarchical grid with two lev
104241
````
105242
````VB
106243
Public Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath("~/Grid/Data/Access/Nwind.mdb")
107-
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles
244+
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles
108245
RadGrid1.NeedDataSource()
109246
RadGrid1.DataSource = GetDataTable("SELECT TOP 5 * From Customers")
110247
End Sub
111248
Public Shared Function GetDataTable(ByVal query As String) As DataTable
112-
249+
113250
Dim connection1 As New OleDbConnection(connectionString)
114251
Dim adapter1 As New OleDbDataAdapter
115252
adapter1.SelectCommand = New OleDbCommand(query, connection1)
@@ -188,8 +325,8 @@ The forthcoming sample implementation is made for hierarchical grid with two lev
188325
e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = \'" + (CustomerID + "\'"));
189326
}
190327
}
191-
protected void btnHideDetailInsert_Click(object sender, System.EventArgs e)
192-
{
328+
protected void btnHideDetailInsert_Click(object sender, System.EventArgs e)
329+
{
193330
GridDataItem masterItem;
194331
foreach (masterItem in RadGrid1.MasterTableView.Items)
195332
{
@@ -198,7 +335,7 @@ The forthcoming sample implementation is made for hierarchical grid with two lev
198335
GridCommandItem commandItem = masterItem.ChildItem.NestedTableViews[0].GetItems(GridItemType.CommandItem)[0];
199336
LinkButton lnkButton = (LinkButton)commandItem.FindControl("lbDetailInsert");
200337
lnkButton.Visible = !lnkButton.Visible;
201-
}
338+
}
202339
}
203340
}
204341
protected void btnDisableMasterInsert_Click(object sender, System.EventArgs e)
@@ -207,7 +344,7 @@ The forthcoming sample implementation is made for hierarchical grid with two lev
207344
LinkButton lnkButton = (LinkButton)commandItem.FindControl("lbMasterInsert");
208345
lnkButton.Enabled = !lnkButton.Enabled;
209346
}
210-
protected void btnReferenceColumns_Click(object sender, System.EventArgs e)
347+
protected void btnReferenceColumns_Click(object sender, System.EventArgs e)
211348
{
212349
GridDataItem masterItem;
213350
foreach (masterItem in RadGrid1.MasterTableView.Items)
@@ -216,10 +353,9 @@ The forthcoming sample implementation is made for hierarchical grid with two lev
216353
{
217354
GridBoundColumn detailColumn = (GridBoundColumn)masterItem.ChildItem.NestedTableViews[0].GetColumnSafe("OrderID");
218355
detailColumn.HeaderStyle.Width = Unit.Pixel(50);
219-
}
356+
}
220357
}
221358
GridBoundColumn masterColumn = (GridBoundColumn)RadGrid1.MasterTableView.GetColumn("CustomerID");
222359
masterColumn.Visible = !masterColumn.Visible;
223360
}
224361
````
225-

0 commit comments

Comments
 (0)