Checkbox Column in RadGridView in Code Behind

Telerik controls are great, but I don’t understand why ‘code behind’ examples are not forthcoming.

Adding a checkbox column to a RadGridView outside of ‘edit mode’  is actually quite easy.. I don’t see any reason why this can’t be added to the documentation.

First, define a template class in your solution such as the one below:


using System;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using Telerik.Web.UI;

namespace WebApplication3

{

public class RadGridCheckBoxTemplate : System.Web.UI.ITemplate

{

/// <summary>

/// Contructor

/// </summary>

/// <param name="type"></param>

public RadGridCheckBoxTemplate()

{

}

/// <summary>

/// Create the template

/// </summary>

/// <param name="container"></param>

public void InstantiateIn(System.Web.UI.Control container)

{

CheckBox checkbox = new CheckBox();

checkbox.ID = "download";

checkbox.CheckedChanged += new EventHandler(ToggleRowSelection);

container.Controls.Add(checkbox);

}

/// <summary>

/// I didn't need this in the end, but if you want to bind to a data source

/// then you will need something like this

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

static void Item_DataBinding(object sender, System.EventArgs e)

{

/*  PlaceHolder ph = (PlaceHolder)sender;

CheckBox ri = (CheckBox)ph.NamingContainer;

bool item1Value = (String)DataBinder.Eval(ri.Checked, "download");

((CheckBox)ph.FindControl("download")).Checked = item1Value;*/

}

/// <summary>

/// Check and uncheck

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void ToggleRowSelection(object sender, EventArgs e)

{

((sender as CheckBox).Parent.Parent as GridItem).Selected = (sender as CheckBox).Checked;

}

}

}
<div></div>

Then add a new GridTemplateColumn to your grid, with the class above as your template:

GridTemplateColumn cbDownload = new GridTemplateColumn();
cbDownload.HeaderText = "Download";
cbDownload.DataField = "Download";
cbDownload.HeaderTooltip = "";
cbDownload.ReadOnly = false;
cbDownload.ItemTemplate = new RadGridCheckBoxTemplate();

And this is how you can check for ‘checked’ values in your gridview:


/// <summary>

/// A button was clicked. Let us loop through the grid looking for checked items.

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

void btnDownload_Click(object sender, EventArgs e)

{

foreach (GridDataItem item in this.gvResults.MasterTableView.Items)

{

if (item.Selected)

{

// Checkbox has marked this row as 'selected'. Find a key vlaue.

string mySelectedString = (string)item.OwnerTableView.DataKeyValues[item.ItemIndex]["MyClassID"];

// Do whatever you want with the selected value...

}

}

}

Advertisements

2 thoughts on “Checkbox Column in RadGridView in Code Behind

  1. Jeeezo! I’ve been wrestling with this for a while. Thanks for posting this, I hope they add this simple example to Telerik’s documentation. Well done.

    1. Thanks! Great to hear it is useful to others.

      I understand why Telerik want to focus on the client side, but sometimes we need to do Code Behind. It should be easier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s