New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Using the GetItems GetColumn and GetColumnSafe Methods

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).

The topic demonstrates accessing the following instances:

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 article.

Items

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:

C#
	    GridItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
	    //cast the item to GridHeaderItem and operate with it further

Data items loaded on the current page can be traversed using the following approach:

C#
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
		foreach (GridDataItem item in RadGrid1.Items)
		{
				if (item.ItemIndex % 3 == 0)
				{
						item["ShipName"].BackColor = System.Drawing.Color.Orange;
				}
		}
}

Edit Items

Currently edited items can be accessed using one of the following methods:

C#
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
		foreach (GridDataItem item in RadGrid1.EditItems)
		{
				// InPlace EditMode
				TextBox textBox1 = item["ShipName"].Controls[0] as TextBox;

				// EditForms and PopUp EditMode
				GridEditFormItem editFormItem = item.EditFormItem;
				if (editFormItem.IsInEditMode)
				{
						TextBox textBox2 = editFormItem["ShipName"].Controls[0] as TextBox;
				}
		}

		// alternative approach
		foreach (GridEditFormItem item in RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
		{
				if (item.IsInEditMode)
				{
						TextBox textBox3 = item["ShipName"].Controls[0] as TextBox;
				}
		}
}

Insert Item

The Insert Item can be accessed using the following method:

C#
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
		if (RadGrid1.MasterTableView.IsItemInserted)
		{
				// GridEditFormInsertItem for EditForms and PopUp EditMode
				// GridDataInsertItem for InPlace EditMode
				GridEditableItem insertItem = RadGrid1.MasterTableView.GetInsertItem();
		}
}

Columns

The GetColumn(columnName) method returns the column with the unique name specified as argument, namely:

C#
	    GridColumn column = RadGrid1.MasterTableView.GetColumn["OrderID"];
	    //thus you get reference to the column with OrderID unique name

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.

The entire column collection can be traversed using the following approach:

C#
protected void RadGrid1_DataBinding(object sender, EventArgs e)
{
		foreach (GridColumn col in RadGrid1.MasterTableView.RenderColumns)
		{
				if (col.DataType == typeof(decimal))
				{
						(col as IGridDataColumn).AllowFiltering = false;
				}
				if (col is GridDateTimeColumn)
				{
						GridDateTimeColumn dateCol = (GridDateTimeColumn)col;
						dateCol.DataFormatString = "{0:d}";
				}
		}
}

The forthcoming sample implementation is made for hierarchical grid with two levels. With separate buttons on the page you can:

  • switch the visibility for the inner tables command item link button (only for expanded parent items)

  • enable/disable the link button inside the master table command item template

  • show/hide the CustomerID column in the main table (calling the GetColumn(columnName) method)

  • modify the width for the OrderID column in each detail table (only for expanded parent items)

The new size for the OrderID column will be retained till the next rebind action in the grid

ASP.NET
	  <asp:LinkButton ID="btnReferenceColumns" runat="server" Text="Operate with columns" /><br />
	  <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AutoGenerateColumns="False">
	    <MasterTableView DataKeyNames="CustomerID" Width="100%" CommandItemDisplay="Top">
	      <CommandItemTemplate>
	        <asp:LinkButton ID="lbMasterInsert" runat="server" CommandName="InitInsert" Text="Insert Customer"></asp:LinkButton>
	      </CommandItemTemplate>
	      <DetailTables>
	        <telerik:GridTableView DataKeyNames="OrderID" Width="100%" runat="server" CommandItemDisplay="Top">
	          <CommandItemTemplate>
	            <asp:LinkButton ID="lbDetailInsert" runat="server" CommandName="InitInsert" Text="Insert Order"></asp:LinkButton>
	          </CommandItemTemplate>
	          <Columns>
	            <telerik:GridBoundColumn HeaderText="OrderID" HeaderButtonType="TextButton" DataField="OrderID"
	              UniqueName="OrderID">
	            </telerik:GridBoundColumn>
	            <telerik:GridBoundColumn HeaderText="Date Ordered" HeaderButtonType="TextButton"
	              DataField="OrderDate" UniqueName="OrderDate">
	            </telerik:GridBoundColumn>
	            <telerik:GridBoundColumn HeaderText="EmployeeID" HeaderButtonType="TextButton" DataField="EmployeeID"
	              UniqueName="EmployeeID">
	            </telerik:GridBoundColumn>
	          </Columns>
	        </telerik:GridTableView>
	      </DetailTables>
	      <Columns>
	        <telerik:GridBoundColumn HeaderText="CustomerID" HeaderButtonType="TextButton" DataField="CustomerID"
	          UniqueName="CustomerID">
	        </telerik:GridBoundColumn>
	        <telerik:GridBoundColumn HeaderText="Contact Name" HeaderButtonType="TextButton"
	          DataField="ContactName" UniqueName="ContactName">
	        </telerik:GridBoundColumn>
	        <telerik:GridBoundColumn HeaderText="Company" HeaderButtonType="TextButton" DataField="CompanyName"
	          UniqueName="CompanyName">
	        </telerik:GridBoundColumn>
	      </Columns>
	    </MasterTableView></telerik:RadGrid><asp:Button ID="btnHideDetailInsert" runat="server"
	      Text="Switch insert option visibility for detail table" /><br />
	  <asp:Button ID="btnDisableMasterInsert" runat="server" Text="Disable/Enable insert for master table" />

See Also